LLVM API Documentation
00001 //===-- Constants.cpp - Implement Constant nodes --------------------------===// 00002 // 00003 // The LLVM Compiler Infrastructure 00004 // 00005 // This file was developed by the LLVM research group and is distributed under 00006 // the University of Illinois Open Source License. See LICENSE.TXT for details. 00007 // 00008 //===----------------------------------------------------------------------===// 00009 // 00010 // This file implements the Constant* classes... 00011 // 00012 //===----------------------------------------------------------------------===// 00013 00014 #include "llvm/Constants.h" 00015 #include "ConstantFolding.h" 00016 #include "llvm/DerivedTypes.h" 00017 #include "llvm/GlobalValue.h" 00018 #include "llvm/Instructions.h" 00019 #include "llvm/SymbolTable.h" 00020 #include "llvm/Module.h" 00021 #include "llvm/ADT/StringExtras.h" 00022 #include "llvm/Support/MathExtras.h" 00023 #include <algorithm> 00024 #include <iostream> 00025 using namespace llvm; 00026 00027 ConstantBool *ConstantBool::True = new ConstantBool(true); 00028 ConstantBool *ConstantBool::False = new ConstantBool(false); 00029 00030 00031 //===----------------------------------------------------------------------===// 00032 // Constant Class 00033 //===----------------------------------------------------------------------===// 00034 00035 void Constant::destroyConstantImpl() { 00036 // When a Constant is destroyed, there may be lingering 00037 // references to the constant by other constants in the constant pool. These 00038 // constants are implicitly dependent on the module that is being deleted, 00039 // but they don't know that. Because we only find out when the CPV is 00040 // deleted, we must now notify all of our users (that should only be 00041 // Constants) that they are, in fact, invalid now and should be deleted. 00042 // 00043 while (!use_empty()) { 00044 Value *V = use_back(); 00045 #ifndef NDEBUG // Only in -g mode... 00046 if (!isa<Constant>(V)) 00047 std::cerr << "While deleting: " << *this 00048 << "\n\nUse still stuck around after Def is destroyed: " 00049 << *V << "\n\n"; 00050 #endif 00051 assert(isa<Constant>(V) && "References remain to Constant being destroyed"); 00052 Constant *CV = cast<Constant>(V); 00053 CV->destroyConstant(); 00054 00055 // The constant should remove itself from our use list... 00056 assert((use_empty() || use_back() != V) && "Constant not removed!"); 00057 } 00058 00059 // Value has no outstanding references it is safe to delete it now... 00060 delete this; 00061 } 00062 00063 // Static constructor to create a '0' constant of arbitrary type... 00064 Constant *Constant::getNullValue(const Type *Ty) { 00065 switch (Ty->getTypeID()) { 00066 case Type::BoolTyID: { 00067 static Constant *NullBool = ConstantBool::get(false); 00068 return NullBool; 00069 } 00070 case Type::SByteTyID: { 00071 static Constant *NullSByte = ConstantSInt::get(Type::SByteTy, 0); 00072 return NullSByte; 00073 } 00074 case Type::UByteTyID: { 00075 static Constant *NullUByte = ConstantUInt::get(Type::UByteTy, 0); 00076 return NullUByte; 00077 } 00078 case Type::ShortTyID: { 00079 static Constant *NullShort = ConstantSInt::get(Type::ShortTy, 0); 00080 return NullShort; 00081 } 00082 case Type::UShortTyID: { 00083 static Constant *NullUShort = ConstantUInt::get(Type::UShortTy, 0); 00084 return NullUShort; 00085 } 00086 case Type::IntTyID: { 00087 static Constant *NullInt = ConstantSInt::get(Type::IntTy, 0); 00088 return NullInt; 00089 } 00090 case Type::UIntTyID: { 00091 static Constant *NullUInt = ConstantUInt::get(Type::UIntTy, 0); 00092 return NullUInt; 00093 } 00094 case Type::LongTyID: { 00095 static Constant *NullLong = ConstantSInt::get(Type::LongTy, 0); 00096 return NullLong; 00097 } 00098 case Type::ULongTyID: { 00099 static Constant *NullULong = ConstantUInt::get(Type::ULongTy, 0); 00100 return NullULong; 00101 } 00102 00103 case Type::FloatTyID: { 00104 static Constant *NullFloat = ConstantFP::get(Type::FloatTy, 0); 00105 return NullFloat; 00106 } 00107 case Type::DoubleTyID: { 00108 static Constant *NullDouble = ConstantFP::get(Type::DoubleTy, 0); 00109 return NullDouble; 00110 } 00111 00112 case Type::PointerTyID: 00113 return ConstantPointerNull::get(cast<PointerType>(Ty)); 00114 00115 case Type::StructTyID: 00116 case Type::ArrayTyID: 00117 case Type::PackedTyID: 00118 return ConstantAggregateZero::get(Ty); 00119 default: 00120 // Function, Label, or Opaque type? 00121 assert(!"Cannot create a null constant of that type!"); 00122 return 0; 00123 } 00124 } 00125 00126 // Static constructor to create the maximum constant of an integral type... 00127 ConstantIntegral *ConstantIntegral::getMaxValue(const Type *Ty) { 00128 switch (Ty->getTypeID()) { 00129 case Type::BoolTyID: return ConstantBool::True; 00130 case Type::SByteTyID: 00131 case Type::ShortTyID: 00132 case Type::IntTyID: 00133 case Type::LongTyID: { 00134 // Calculate 011111111111111... 00135 unsigned TypeBits = Ty->getPrimitiveSize()*8; 00136 int64_t Val = INT64_MAX; // All ones 00137 Val >>= 64-TypeBits; // Shift out unwanted 1 bits... 00138 return ConstantSInt::get(Ty, Val); 00139 } 00140 00141 case Type::UByteTyID: 00142 case Type::UShortTyID: 00143 case Type::UIntTyID: 00144 case Type::ULongTyID: return getAllOnesValue(Ty); 00145 00146 default: return 0; 00147 } 00148 } 00149 00150 // Static constructor to create the minimum constant for an integral type... 00151 ConstantIntegral *ConstantIntegral::getMinValue(const Type *Ty) { 00152 switch (Ty->getTypeID()) { 00153 case Type::BoolTyID: return ConstantBool::False; 00154 case Type::SByteTyID: 00155 case Type::ShortTyID: 00156 case Type::IntTyID: 00157 case Type::LongTyID: { 00158 // Calculate 1111111111000000000000 00159 unsigned TypeBits = Ty->getPrimitiveSize()*8; 00160 int64_t Val = -1; // All ones 00161 Val <<= TypeBits-1; // Shift over to the right spot 00162 return ConstantSInt::get(Ty, Val); 00163 } 00164 00165 case Type::UByteTyID: 00166 case Type::UShortTyID: 00167 case Type::UIntTyID: 00168 case Type::ULongTyID: return ConstantUInt::get(Ty, 0); 00169 00170 default: return 0; 00171 } 00172 } 00173 00174 // Static constructor to create an integral constant with all bits set 00175 ConstantIntegral *ConstantIntegral::getAllOnesValue(const Type *Ty) { 00176 switch (Ty->getTypeID()) { 00177 case Type::BoolTyID: return ConstantBool::True; 00178 case Type::SByteTyID: 00179 case Type::ShortTyID: 00180 case Type::IntTyID: 00181 case Type::LongTyID: return ConstantSInt::get(Ty, -1); 00182 00183 case Type::UByteTyID: 00184 case Type::UShortTyID: 00185 case Type::UIntTyID: 00186 case Type::ULongTyID: { 00187 // Calculate ~0 of the right type... 00188 unsigned TypeBits = Ty->getPrimitiveSize()*8; 00189 uint64_t Val = ~0ULL; // All ones 00190 Val >>= 64-TypeBits; // Shift out unwanted 1 bits... 00191 return ConstantUInt::get(Ty, Val); 00192 } 00193 default: return 0; 00194 } 00195 } 00196 00197 bool ConstantUInt::isAllOnesValue() const { 00198 unsigned TypeBits = getType()->getPrimitiveSize()*8; 00199 uint64_t Val = ~0ULL; // All ones 00200 Val >>= 64-TypeBits; // Shift out inappropriate bits 00201 return getValue() == Val; 00202 } 00203 00204 00205 //===----------------------------------------------------------------------===// 00206 // ConstantXXX Classes 00207 //===----------------------------------------------------------------------===// 00208 00209 //===----------------------------------------------------------------------===// 00210 // Normal Constructors 00211 00212 ConstantIntegral::ConstantIntegral(const Type *Ty, ValueTy VT, uint64_t V) 00213 : Constant(Ty, VT, 0, 0) { 00214 Val.Unsigned = V; 00215 } 00216 00217 ConstantBool::ConstantBool(bool V) 00218 : ConstantIntegral(Type::BoolTy, ConstantBoolVal, V) { 00219 } 00220 00221 ConstantInt::ConstantInt(const Type *Ty, ValueTy VT, uint64_t V) 00222 : ConstantIntegral(Ty, VT, V) { 00223 } 00224 00225 ConstantSInt::ConstantSInt(const Type *Ty, int64_t V) 00226 : ConstantInt(Ty, ConstantSIntVal, V) { 00227 assert(Ty->isInteger() && Ty->isSigned() && 00228 "Illegal type for signed integer constant!"); 00229 assert(isValueValidForType(Ty, V) && "Value too large for type!"); 00230 } 00231 00232 ConstantUInt::ConstantUInt(const Type *Ty, uint64_t V) 00233 : ConstantInt(Ty, ConstantUIntVal, V) { 00234 assert(Ty->isInteger() && Ty->isUnsigned() && 00235 "Illegal type for unsigned integer constant!"); 00236 assert(isValueValidForType(Ty, V) && "Value too large for type!"); 00237 } 00238 00239 ConstantFP::ConstantFP(const Type *Ty, double V) 00240 : Constant(Ty, ConstantFPVal, 0, 0) { 00241 assert(isValueValidForType(Ty, V) && "Value too large for type!"); 00242 Val = V; 00243 } 00244 00245 ConstantArray::ConstantArray(const ArrayType *T, 00246 const std::vector<Constant*> &V) 00247 : Constant(T, ConstantArrayVal, new Use[V.size()], V.size()) { 00248 assert(V.size() == T->getNumElements() && 00249 "Invalid initializer vector for constant array"); 00250 Use *OL = OperandList; 00251 for (std::vector<Constant*>::const_iterator I = V.begin(), E = V.end(); 00252 I != E; ++I, ++OL) { 00253 Constant *C = *I; 00254 assert((C->getType() == T->getElementType() || 00255 (T->isAbstract() && 00256 C->getType()->getTypeID() == T->getElementType()->getTypeID())) && 00257 "Initializer for array element doesn't match array element type!"); 00258 OL->init(C, this); 00259 } 00260 } 00261 00262 ConstantArray::~ConstantArray() { 00263 delete [] OperandList; 00264 } 00265 00266 ConstantStruct::ConstantStruct(const StructType *T, 00267 const std::vector<Constant*> &V) 00268 : Constant(T, ConstantStructVal, new Use[V.size()], V.size()) { 00269 assert(V.size() == T->getNumElements() && 00270 "Invalid initializer vector for constant structure"); 00271 Use *OL = OperandList; 00272 for (std::vector<Constant*>::const_iterator I = V.begin(), E = V.end(); 00273 I != E; ++I, ++OL) { 00274 Constant *C = *I; 00275 assert((C->getType() == T->getElementType(I-V.begin()) || 00276 ((T->getElementType(I-V.begin())->isAbstract() || 00277 C->getType()->isAbstract()) && 00278 T->getElementType(I-V.begin())->getTypeID() == 00279 C->getType()->getTypeID())) && 00280 "Initializer for struct element doesn't match struct element type!"); 00281 OL->init(C, this); 00282 } 00283 } 00284 00285 ConstantStruct::~ConstantStruct() { 00286 delete [] OperandList; 00287 } 00288 00289 00290 ConstantPacked::ConstantPacked(const PackedType *T, 00291 const std::vector<Constant*> &V) 00292 : Constant(T, ConstantPackedVal, new Use[V.size()], V.size()) { 00293 Use *OL = OperandList; 00294 for (std::vector<Constant*>::const_iterator I = V.begin(), E = V.end(); 00295 I != E; ++I, ++OL) { 00296 Constant *C = *I; 00297 assert((C->getType() == T->getElementType() || 00298 (T->isAbstract() && 00299 C->getType()->getTypeID() == T->getElementType()->getTypeID())) && 00300 "Initializer for packed element doesn't match packed element type!"); 00301 OL->init(C, this); 00302 } 00303 } 00304 00305 ConstantPacked::~ConstantPacked() { 00306 delete [] OperandList; 00307 } 00308 00309 /// UnaryConstantExpr - This class is private to Constants.cpp, and is used 00310 /// behind the scenes to implement unary constant exprs. 00311 class UnaryConstantExpr : public ConstantExpr { 00312 Use Op; 00313 public: 00314 UnaryConstantExpr(unsigned Opcode, Constant *C, const Type *Ty) 00315 : ConstantExpr(Ty, Opcode, &Op, 1), Op(C, this) {} 00316 }; 00317 00318 static bool isSetCC(unsigned Opcode) { 00319 return Opcode == Instruction::SetEQ || Opcode == Instruction::SetNE || 00320 Opcode == Instruction::SetLT || Opcode == Instruction::SetGT || 00321 Opcode == Instruction::SetLE || Opcode == Instruction::SetGE; 00322 } 00323 00324 /// BinaryConstantExpr - This class is private to Constants.cpp, and is used 00325 /// behind the scenes to implement binary constant exprs. 00326 class BinaryConstantExpr : public ConstantExpr { 00327 Use Ops[2]; 00328 public: 00329 BinaryConstantExpr(unsigned Opcode, Constant *C1, Constant *C2) 00330 : ConstantExpr(isSetCC(Opcode) ? Type::BoolTy : C1->getType(), 00331 Opcode, Ops, 2) { 00332 Ops[0].init(C1, this); 00333 Ops[1].init(C2, this); 00334 } 00335 }; 00336 00337 /// SelectConstantExpr - This class is private to Constants.cpp, and is used 00338 /// behind the scenes to implement select constant exprs. 00339 class SelectConstantExpr : public ConstantExpr { 00340 Use Ops[3]; 00341 public: 00342 SelectConstantExpr(Constant *C1, Constant *C2, Constant *C3) 00343 : ConstantExpr(C2->getType(), Instruction::Select, Ops, 3) { 00344 Ops[0].init(C1, this); 00345 Ops[1].init(C2, this); 00346 Ops[2].init(C3, this); 00347 } 00348 }; 00349 00350 /// ExtractElementConstantExpr - This class is private to 00351 /// Constants.cpp, and is used behind the scenes to implement 00352 /// extractelement constant exprs. 00353 class ExtractElementConstantExpr : public ConstantExpr { 00354 Use Ops[2]; 00355 public: 00356 ExtractElementConstantExpr(Constant *C1, Constant *C2) 00357 : ConstantExpr(cast<PackedType>(C1->getType())->getElementType(), 00358 Instruction::ExtractElement, Ops, 2) { 00359 Ops[0].init(C1, this); 00360 Ops[1].init(C2, this); 00361 } 00362 }; 00363 00364 /// InsertElementConstantExpr - This class is private to 00365 /// Constants.cpp, and is used behind the scenes to implement 00366 /// insertelement constant exprs. 00367 class InsertElementConstantExpr : public ConstantExpr { 00368 Use Ops[3]; 00369 public: 00370 InsertElementConstantExpr(Constant *C1, Constant *C2, Constant *C3) 00371 : ConstantExpr(C1->getType(), Instruction::InsertElement, 00372 Ops, 3) { 00373 Ops[0].init(C1, this); 00374 Ops[1].init(C2, this); 00375 Ops[2].init(C3, this); 00376 } 00377 }; 00378 00379 /// ShuffleVectorConstantExpr - This class is private to 00380 /// Constants.cpp, and is used behind the scenes to implement 00381 /// shufflevector constant exprs. 00382 class ShuffleVectorConstantExpr : public ConstantExpr { 00383 Use Ops[3]; 00384 public: 00385 ShuffleVectorConstantExpr(Constant *C1, Constant *C2, Constant *C3) 00386 : ConstantExpr(C1->getType(), Instruction::ShuffleVector, 00387 Ops, 3) { 00388 Ops[0].init(C1, this); 00389 Ops[1].init(C2, this); 00390 Ops[2].init(C3, this); 00391 } 00392 }; 00393 00394 /// GetElementPtrConstantExpr - This class is private to Constants.cpp, and is 00395 /// used behind the scenes to implement getelementpr constant exprs. 00396 struct GetElementPtrConstantExpr : public ConstantExpr { 00397 GetElementPtrConstantExpr(Constant *C, const std::vector<Constant*> &IdxList, 00398 const Type *DestTy) 00399 : ConstantExpr(DestTy, Instruction::GetElementPtr, 00400 new Use[IdxList.size()+1], IdxList.size()+1) { 00401 OperandList[0].init(C, this); 00402 for (unsigned i = 0, E = IdxList.size(); i != E; ++i) 00403 OperandList[i+1].init(IdxList[i], this); 00404 } 00405 ~GetElementPtrConstantExpr() { 00406 delete [] OperandList; 00407 } 00408 }; 00409 00410 /// ConstantExpr::get* - Return some common constants without having to 00411 /// specify the full Instruction::OPCODE identifier. 00412 /// 00413 Constant *ConstantExpr::getNeg(Constant *C) { 00414 if (!C->getType()->isFloatingPoint()) 00415 return get(Instruction::Sub, getNullValue(C->getType()), C); 00416 else 00417 return get(Instruction::Sub, ConstantFP::get(C->getType(), -0.0), C); 00418 } 00419 Constant *ConstantExpr::getNot(Constant *C) { 00420 assert(isa<ConstantIntegral>(C) && "Cannot NOT a nonintegral type!"); 00421 return get(Instruction::Xor, C, 00422 ConstantIntegral::getAllOnesValue(C->getType())); 00423 } 00424 Constant *ConstantExpr::getAdd(Constant *C1, Constant *C2) { 00425 return get(Instruction::Add, C1, C2); 00426 } 00427 Constant *ConstantExpr::getSub(Constant *C1, Constant *C2) { 00428 return get(Instruction::Sub, C1, C2); 00429 } 00430 Constant *ConstantExpr::getMul(Constant *C1, Constant *C2) { 00431 return get(Instruction::Mul, C1, C2); 00432 } 00433 Constant *ConstantExpr::getDiv(Constant *C1, Constant *C2) { 00434 return get(Instruction::Div, C1, C2); 00435 } 00436 Constant *ConstantExpr::getRem(Constant *C1, Constant *C2) { 00437 return get(Instruction::Rem, C1, C2); 00438 } 00439 Constant *ConstantExpr::getAnd(Constant *C1, Constant *C2) { 00440 return get(Instruction::And, C1, C2); 00441 } 00442 Constant *ConstantExpr::getOr(Constant *C1, Constant *C2) { 00443 return get(Instruction::Or, C1, C2); 00444 } 00445 Constant *ConstantExpr::getXor(Constant *C1, Constant *C2) { 00446 return get(Instruction::Xor, C1, C2); 00447 } 00448 Constant *ConstantExpr::getSetEQ(Constant *C1, Constant *C2) { 00449 return get(Instruction::SetEQ, C1, C2); 00450 } 00451 Constant *ConstantExpr::getSetNE(Constant *C1, Constant *C2) { 00452 return get(Instruction::SetNE, C1, C2); 00453 } 00454 Constant *ConstantExpr::getSetLT(Constant *C1, Constant *C2) { 00455 return get(Instruction::SetLT, C1, C2); 00456 } 00457 Constant *ConstantExpr::getSetGT(Constant *C1, Constant *C2) { 00458 return get(Instruction::SetGT, C1, C2); 00459 } 00460 Constant *ConstantExpr::getSetLE(Constant *C1, Constant *C2) { 00461 return get(Instruction::SetLE, C1, C2); 00462 } 00463 Constant *ConstantExpr::getSetGE(Constant *C1, Constant *C2) { 00464 return get(Instruction::SetGE, C1, C2); 00465 } 00466 Constant *ConstantExpr::getShl(Constant *C1, Constant *C2) { 00467 return get(Instruction::Shl, C1, C2); 00468 } 00469 Constant *ConstantExpr::getShr(Constant *C1, Constant *C2) { 00470 return get(Instruction::Shr, C1, C2); 00471 } 00472 00473 Constant *ConstantExpr::getUShr(Constant *C1, Constant *C2) { 00474 if (C1->getType()->isUnsigned()) return getShr(C1, C2); 00475 return getCast(getShr(getCast(C1, 00476 C1->getType()->getUnsignedVersion()), C2), C1->getType()); 00477 } 00478 00479 Constant *ConstantExpr::getSShr(Constant *C1, Constant *C2) { 00480 if (C1->getType()->isSigned()) return getShr(C1, C2); 00481 return getCast(getShr(getCast(C1, 00482 C1->getType()->getSignedVersion()), C2), C1->getType()); 00483 } 00484 00485 00486 //===----------------------------------------------------------------------===// 00487 // isValueValidForType implementations 00488 00489 bool ConstantSInt::isValueValidForType(const Type *Ty, int64_t Val) { 00490 switch (Ty->getTypeID()) { 00491 default: 00492 return false; // These can't be represented as integers!!! 00493 // Signed types... 00494 case Type::SByteTyID: 00495 return (Val <= INT8_MAX && Val >= INT8_MIN); 00496 case Type::ShortTyID: 00497 return (Val <= INT16_MAX && Val >= INT16_MIN); 00498 case Type::IntTyID: 00499 return (Val <= int(INT32_MAX) && Val >= int(INT32_MIN)); 00500 case Type::LongTyID: 00501 return true; // This is the largest type... 00502 } 00503 } 00504 00505 bool ConstantUInt::isValueValidForType(const Type *Ty, uint64_t Val) { 00506 switch (Ty->getTypeID()) { 00507 default: 00508 return false; // These can't be represented as integers!!! 00509 00510 // Unsigned types... 00511 case Type::UByteTyID: 00512 return (Val <= UINT8_MAX); 00513 case Type::UShortTyID: 00514 return (Val <= UINT16_MAX); 00515 case Type::UIntTyID: 00516 return (Val <= UINT32_MAX); 00517 case Type::ULongTyID: 00518 return true; // This is the largest type... 00519 } 00520 } 00521 00522 bool ConstantFP::isValueValidForType(const Type *Ty, double Val) { 00523 switch (Ty->getTypeID()) { 00524 default: 00525 return false; // These can't be represented as floating point! 00526 00527 // TODO: Figure out how to test if a double can be cast to a float! 00528 case Type::FloatTyID: 00529 case Type::DoubleTyID: 00530 return true; // This is the largest type... 00531 } 00532 }; 00533 00534 //===----------------------------------------------------------------------===// 00535 // Factory Function Implementation 00536 00537 // ConstantCreator - A class that is used to create constants by 00538 // ValueMap*. This class should be partially specialized if there is 00539 // something strange that needs to be done to interface to the ctor for the 00540 // constant. 00541 // 00542 namespace llvm { 00543 template<class ConstantClass, class TypeClass, class ValType> 00544 struct ConstantCreator { 00545 static ConstantClass *create(const TypeClass *Ty, const ValType &V) { 00546 return new ConstantClass(Ty, V); 00547 } 00548 }; 00549 00550 template<class ConstantClass, class TypeClass> 00551 struct ConvertConstantType { 00552 static void convert(ConstantClass *OldC, const TypeClass *NewTy) { 00553 assert(0 && "This type cannot be converted!\n"); 00554 abort(); 00555 } 00556 }; 00557 } 00558 00559 namespace { 00560 template<class ValType, class TypeClass, class ConstantClass, 00561 bool HasLargeKey = false /*true for arrays and structs*/ > 00562 class ValueMap : public AbstractTypeUser { 00563 public: 00564 typedef std::pair<const TypeClass*, ValType> MapKey; 00565 typedef std::map<MapKey, ConstantClass *> MapTy; 00566 typedef typename MapTy::iterator MapIterator; 00567 private: 00568 /// Map - This is the main map from the element descriptor to the Constants. 00569 /// This is the primary way we avoid creating two of the same shape 00570 /// constant. 00571 MapTy Map; 00572 00573 /// InverseMap - If "HasLargeKey" is true, this contains an inverse mapping 00574 /// from the constants to their element in Map. This is important for 00575 /// removal of constants from the array, which would otherwise have to scan 00576 /// through the map with very large keys. 00577 std::map<ConstantClass*, MapIterator> InverseMap; 00578 00579 typedef std::map<const TypeClass*, MapIterator> AbstractTypeMapTy; 00580 AbstractTypeMapTy AbstractTypeMap; 00581 00582 friend void Constant::clearAllValueMaps(); 00583 private: 00584 void clear(std::vector<Constant *> &Constants) { 00585 for(MapIterator I = Map.begin(); I != Map.end(); ++I) 00586 Constants.push_back(I->second); 00587 Map.clear(); 00588 AbstractTypeMap.clear(); 00589 InverseMap.clear(); 00590 } 00591 00592 public: 00593 MapIterator map_end() { return Map.end(); } 00594 00595 /// InsertOrGetItem - Return an iterator for the specified element. 00596 /// If the element exists in the map, the returned iterator points to the 00597 /// entry and Exists=true. If not, the iterator points to the newly 00598 /// inserted entry and returns Exists=false. Newly inserted entries have 00599 /// I->second == 0, and should be filled in. 00600 MapIterator InsertOrGetItem(std::pair<MapKey, ConstantClass *> &InsertVal, 00601 bool &Exists) { 00602 std::pair<MapIterator, bool> IP = Map.insert(InsertVal); 00603 Exists = !IP.second; 00604 return IP.first; 00605 } 00606 00607 private: 00608 MapIterator FindExistingElement(ConstantClass *CP) { 00609 if (HasLargeKey) { 00610 typename std::map<ConstantClass*, MapIterator>::iterator 00611 IMI = InverseMap.find(CP); 00612 assert(IMI != InverseMap.end() && IMI->second != Map.end() && 00613 IMI->second->second == CP && 00614 "InverseMap corrupt!"); 00615 return IMI->second; 00616 } 00617 00618 MapIterator I = 00619 Map.find(MapKey((TypeClass*)CP->getRawType(), getValType(CP))); 00620 if (I == Map.end() || I->second != CP) { 00621 // FIXME: This should not use a linear scan. If this gets to be a 00622 // performance problem, someone should look at this. 00623 for (I = Map.begin(); I != Map.end() && I->second != CP; ++I) 00624 /* empty */; 00625 } 00626 return I; 00627 } 00628 public: 00629 00630 /// getOrCreate - Return the specified constant from the map, creating it if 00631 /// necessary. 00632 ConstantClass *getOrCreate(const TypeClass *Ty, const ValType &V) { 00633 MapKey Lookup(Ty, V); 00634 MapIterator I = Map.lower_bound(Lookup); 00635 if (I != Map.end() && I->first == Lookup) 00636 return I->second; // Is it in the map? 00637 00638 // If no preexisting value, create one now... 00639 ConstantClass *Result = 00640 ConstantCreator<ConstantClass,TypeClass,ValType>::create(Ty, V); 00641 00642 /// FIXME: why does this assert fail when loading 176.gcc? 00643 //assert(Result->getType() == Ty && "Type specified is not correct!"); 00644 I = Map.insert(I, std::make_pair(MapKey(Ty, V), Result)); 00645 00646 if (HasLargeKey) // Remember the reverse mapping if needed. 00647 InverseMap.insert(std::make_pair(Result, I)); 00648 00649 // If the type of the constant is abstract, make sure that an entry exists 00650 // for it in the AbstractTypeMap. 00651 if (Ty->isAbstract()) { 00652 typename AbstractTypeMapTy::iterator TI = 00653 AbstractTypeMap.lower_bound(Ty); 00654 00655 if (TI == AbstractTypeMap.end() || TI->first != Ty) { 00656 // Add ourselves to the ATU list of the type. 00657 cast<DerivedType>(Ty)->addAbstractTypeUser(this); 00658 00659 AbstractTypeMap.insert(TI, std::make_pair(Ty, I)); 00660 } 00661 } 00662 return Result; 00663 } 00664 00665 void remove(ConstantClass *CP) { 00666 MapIterator I = FindExistingElement(CP); 00667 assert(I != Map.end() && "Constant not found in constant table!"); 00668 assert(I->second == CP && "Didn't find correct element?"); 00669 00670 if (HasLargeKey) // Remember the reverse mapping if needed. 00671 InverseMap.erase(CP); 00672 00673 // Now that we found the entry, make sure this isn't the entry that 00674 // the AbstractTypeMap points to. 00675 const TypeClass *Ty = I->first.first; 00676 if (Ty->isAbstract()) { 00677 assert(AbstractTypeMap.count(Ty) && 00678 "Abstract type not in AbstractTypeMap?"); 00679 MapIterator &ATMEntryIt = AbstractTypeMap[Ty]; 00680 if (ATMEntryIt == I) { 00681 // Yes, we are removing the representative entry for this type. 00682 // See if there are any other entries of the same type. 00683 MapIterator TmpIt = ATMEntryIt; 00684 00685 // First check the entry before this one... 00686 if (TmpIt != Map.begin()) { 00687 --TmpIt; 00688 if (TmpIt->first.first != Ty) // Not the same type, move back... 00689 ++TmpIt; 00690 } 00691 00692 // If we didn't find the same type, try to move forward... 00693 if (TmpIt == ATMEntryIt) { 00694 ++TmpIt; 00695 if (TmpIt == Map.end() || TmpIt->first.first != Ty) 00696 --TmpIt; // No entry afterwards with the same type 00697 } 00698 00699 // If there is another entry in the map of the same abstract type, 00700 // update the AbstractTypeMap entry now. 00701 if (TmpIt != ATMEntryIt) { 00702 ATMEntryIt = TmpIt; 00703 } else { 00704 // Otherwise, we are removing the last instance of this type 00705 // from the table. Remove from the ATM, and from user list. 00706 cast<DerivedType>(Ty)->removeAbstractTypeUser(this); 00707 AbstractTypeMap.erase(Ty); 00708 } 00709 } 00710 } 00711 00712 Map.erase(I); 00713 } 00714 00715 00716 /// MoveConstantToNewSlot - If we are about to change C to be the element 00717 /// specified by I, update our internal data structures to reflect this 00718 /// fact. 00719 void MoveConstantToNewSlot(ConstantClass *C, MapIterator I) { 00720 // First, remove the old location of the specified constant in the map. 00721 MapIterator OldI = FindExistingElement(C); 00722 assert(OldI != Map.end() && "Constant not found in constant table!"); 00723 assert(OldI->second == C && "Didn't find correct element?"); 00724 00725 // If this constant is the representative element for its abstract type, 00726 // update the AbstractTypeMap so that the representative element is I. 00727 if (C->getType()->isAbstract()) { 00728 typename AbstractTypeMapTy::iterator ATI = 00729 AbstractTypeMap.find(C->getType()); 00730 assert(ATI != AbstractTypeMap.end() && 00731 "Abstract type not in AbstractTypeMap?"); 00732 if (ATI->second == OldI) 00733 ATI->second = I; 00734 } 00735 00736 // Remove the old entry from the map. 00737 Map.erase(OldI); 00738 00739 // Update the inverse map so that we know that this constant is now 00740 // located at descriptor I. 00741 if (HasLargeKey) { 00742 assert(I->second == C && "Bad inversemap entry!"); 00743 InverseMap[C] = I; 00744 } 00745 } 00746 00747 void refineAbstractType(const DerivedType *OldTy, const Type *NewTy) { 00748 typename AbstractTypeMapTy::iterator I = 00749 AbstractTypeMap.find(cast<TypeClass>(OldTy)); 00750 00751 assert(I != AbstractTypeMap.end() && 00752 "Abstract type not in AbstractTypeMap?"); 00753 00754 // Convert a constant at a time until the last one is gone. The last one 00755 // leaving will remove() itself, causing the AbstractTypeMapEntry to be 00756 // eliminated eventually. 00757 do { 00758 ConvertConstantType<ConstantClass, 00759 TypeClass>::convert(I->second->second, 00760 cast<TypeClass>(NewTy)); 00761 00762 I = AbstractTypeMap.find(cast<TypeClass>(OldTy)); 00763 } while (I != AbstractTypeMap.end()); 00764 } 00765 00766 // If the type became concrete without being refined to any other existing 00767 // type, we just remove ourselves from the ATU list. 00768 void typeBecameConcrete(const DerivedType *AbsTy) { 00769 AbsTy->removeAbstractTypeUser(this); 00770 } 00771 00772 void dump() const { 00773 std::cerr << "Constant.cpp: ValueMap\n"; 00774 } 00775 }; 00776 } 00777 00778 //---- ConstantUInt::get() and ConstantSInt::get() implementations... 00779 // 00780 static ValueMap< int64_t, Type, ConstantSInt> SIntConstants; 00781 static ValueMap<uint64_t, Type, ConstantUInt> UIntConstants; 00782 00783 ConstantSInt *ConstantSInt::get(const Type *Ty, int64_t V) { 00784 return SIntConstants.getOrCreate(Ty, V); 00785 } 00786 00787 ConstantUInt *ConstantUInt::get(const Type *Ty, uint64_t V) { 00788 return UIntConstants.getOrCreate(Ty, V); 00789 } 00790 00791 ConstantInt *ConstantInt::get(const Type *Ty, unsigned char V) { 00792 assert(V <= 127 && "Can only be used with very small positive constants!"); 00793 if (Ty->isSigned()) return ConstantSInt::get(Ty, V); 00794 return ConstantUInt::get(Ty, V); 00795 } 00796 00797 //---- ConstantFP::get() implementation... 00798 // 00799 namespace llvm { 00800 template<> 00801 struct ConstantCreator<ConstantFP, Type, uint64_t> { 00802 static ConstantFP *create(const Type *Ty, uint64_t V) { 00803 assert(Ty == Type::DoubleTy); 00804 return new ConstantFP(Ty, BitsToDouble(V)); 00805 } 00806 }; 00807 template<> 00808 struct ConstantCreator<ConstantFP, Type, uint32_t> { 00809 static ConstantFP *create(const Type *Ty, uint32_t V) { 00810 assert(Ty == Type::FloatTy); 00811 return new ConstantFP(Ty, BitsToFloat(V)); 00812 } 00813 }; 00814 } 00815 00816 static ValueMap<uint64_t, Type, ConstantFP> DoubleConstants; 00817 static ValueMap<uint32_t, Type, ConstantFP> FloatConstants; 00818 00819 bool ConstantFP::isNullValue() const { 00820 return DoubleToBits(Val) == 0; 00821 } 00822 00823 bool ConstantFP::isExactlyValue(double V) const { 00824 return DoubleToBits(V) == DoubleToBits(Val); 00825 } 00826 00827 00828 ConstantFP *ConstantFP::get(const Type *Ty, double V) { 00829 if (Ty == Type::FloatTy) { 00830 // Force the value through memory to normalize it. 00831 return FloatConstants.getOrCreate(Ty, FloatToBits(V)); 00832 } else { 00833 assert(Ty == Type::DoubleTy); 00834 return DoubleConstants.getOrCreate(Ty, DoubleToBits(V)); 00835 } 00836 } 00837 00838 //---- ConstantAggregateZero::get() implementation... 00839 // 00840 namespace llvm { 00841 // ConstantAggregateZero does not take extra "value" argument... 00842 template<class ValType> 00843 struct ConstantCreator<ConstantAggregateZero, Type, ValType> { 00844 static ConstantAggregateZero *create(const Type *Ty, const ValType &V){ 00845 return new ConstantAggregateZero(Ty); 00846 } 00847 }; 00848 00849 template<> 00850 struct ConvertConstantType<ConstantAggregateZero, Type> { 00851 static void convert(ConstantAggregateZero *OldC, const Type *NewTy) { 00852 // Make everyone now use a constant of the new type... 00853 Constant *New = ConstantAggregateZero::get(NewTy); 00854 assert(New != OldC && "Didn't replace constant??"); 00855 OldC->uncheckedReplaceAllUsesWith(New); 00856 OldC->destroyConstant(); // This constant is now dead, destroy it. 00857 } 00858 }; 00859 } 00860 00861 static ValueMap<char, Type, ConstantAggregateZero> AggZeroConstants; 00862 00863 static char getValType(ConstantAggregateZero *CPZ) { return 0; } 00864 00865 Constant *ConstantAggregateZero::get(const Type *Ty) { 00866 return AggZeroConstants.getOrCreate(Ty, 0); 00867 } 00868 00869 // destroyConstant - Remove the constant from the constant table... 00870 // 00871 void ConstantAggregateZero::destroyConstant() { 00872 AggZeroConstants.remove(this); 00873 destroyConstantImpl(); 00874 } 00875 00876 //---- ConstantArray::get() implementation... 00877 // 00878 namespace llvm { 00879 template<> 00880 struct ConvertConstantType<ConstantArray, ArrayType> { 00881 static void convert(ConstantArray *OldC, const ArrayType *NewTy) { 00882 // Make everyone now use a constant of the new type... 00883 std::vector<Constant*> C; 00884 for (unsigned i = 0, e = OldC->getNumOperands(); i != e; ++i) 00885 C.push_back(cast<Constant>(OldC->getOperand(i))); 00886 Constant *New = ConstantArray::get(NewTy, C); 00887 assert(New != OldC && "Didn't replace constant??"); 00888 OldC->uncheckedReplaceAllUsesWith(New); 00889 OldC->destroyConstant(); // This constant is now dead, destroy it. 00890 } 00891 }; 00892 } 00893 00894 static std::vector<Constant*> getValType(ConstantArray *CA) { 00895 std::vector<Constant*> Elements; 00896 Elements.reserve(CA->getNumOperands()); 00897 for (unsigned i = 0, e = CA->getNumOperands(); i != e; ++i) 00898 Elements.push_back(cast<Constant>(CA->getOperand(i))); 00899 return Elements; 00900 } 00901 00902 typedef ValueMap<std::vector<Constant*>, ArrayType, 00903 ConstantArray, true /*largekey*/> ArrayConstantsTy; 00904 static ArrayConstantsTy ArrayConstants; 00905 00906 Constant *ConstantArray::get(const ArrayType *Ty, 00907 const std::vector<Constant*> &V) { 00908 // If this is an all-zero array, return a ConstantAggregateZero object 00909 if (!V.empty()) { 00910 Constant *C = V[0]; 00911 if (!C->isNullValue()) 00912 return ArrayConstants.getOrCreate(Ty, V); 00913 for (unsigned i = 1, e = V.size(); i != e; ++i) 00914 if (V[i] != C) 00915 return ArrayConstants.getOrCreate(Ty, V); 00916 } 00917 return ConstantAggregateZero::get(Ty); 00918 } 00919 00920 // destroyConstant - Remove the constant from the constant table... 00921 // 00922 void ConstantArray::destroyConstant() { 00923 ArrayConstants.remove(this); 00924 destroyConstantImpl(); 00925 } 00926 00927 // ConstantArray::get(const string&) - Return an array that is initialized to 00928 // contain the specified string. A null terminator is added to the specified 00929 // string so that it may be used in a natural way... 00930 // 00931 Constant *ConstantArray::get(const std::string &Str) { 00932 std::vector<Constant*> ElementVals; 00933 00934 for (unsigned i = 0; i < Str.length(); ++i) 00935 ElementVals.push_back(ConstantSInt::get(Type::SByteTy, Str[i])); 00936 00937 // Add a null terminator to the string... 00938 ElementVals.push_back(ConstantSInt::get(Type::SByteTy, 0)); 00939 00940 ArrayType *ATy = ArrayType::get(Type::SByteTy, Str.length()+1); 00941 return ConstantArray::get(ATy, ElementVals); 00942 } 00943 00944 /// isString - This method returns true if the array is an array of sbyte or 00945 /// ubyte, and if the elements of the array are all ConstantInt's. 00946 bool ConstantArray::isString() const { 00947 // Check the element type for sbyte or ubyte... 00948 if (getType()->getElementType() != Type::UByteTy && 00949 getType()->getElementType() != Type::SByteTy) 00950 return false; 00951 // Check the elements to make sure they are all integers, not constant 00952 // expressions. 00953 for (unsigned i = 0, e = getNumOperands(); i != e; ++i) 00954 if (!isa<ConstantInt>(getOperand(i))) 00955 return false; 00956 return true; 00957 } 00958 00959 // getAsString - If the sub-element type of this array is either sbyte or ubyte, 00960 // then this method converts the array to an std::string and returns it. 00961 // Otherwise, it asserts out. 00962 // 00963 std::string ConstantArray::getAsString() const { 00964 assert(isString() && "Not a string!"); 00965 std::string Result; 00966 for (unsigned i = 0, e = getNumOperands(); i != e; ++i) 00967 Result += (char)cast<ConstantInt>(getOperand(i))->getRawValue(); 00968 return Result; 00969 } 00970 00971 00972 //---- ConstantStruct::get() implementation... 00973 // 00974 00975 namespace llvm { 00976 template<> 00977 struct ConvertConstantType<ConstantStruct, StructType> { 00978 static void convert(ConstantStruct *OldC, const StructType *NewTy) { 00979 // Make everyone now use a constant of the new type... 00980 std::vector<Constant*> C; 00981 for (unsigned i = 0, e = OldC->getNumOperands(); i != e; ++i) 00982 C.push_back(cast<Constant>(OldC->getOperand(i))); 00983 Constant *New = ConstantStruct::get(NewTy, C); 00984 assert(New != OldC && "Didn't replace constant??"); 00985 00986 OldC->uncheckedReplaceAllUsesWith(New); 00987 OldC->destroyConstant(); // This constant is now dead, destroy it. 00988 } 00989 }; 00990 } 00991 00992 typedef ValueMap<std::vector<Constant*>, StructType, 00993 ConstantStruct, true /*largekey*/> StructConstantsTy; 00994 static StructConstantsTy StructConstants; 00995 00996 static std::vector<Constant*> getValType(ConstantStruct *CS) { 00997 std::vector<Constant*> Elements; 00998 Elements.reserve(CS->getNumOperands()); 00999 for (unsigned i = 0, e = CS->getNumOperands(); i != e; ++i) 01000 Elements.push_back(cast<Constant>(CS->getOperand(i))); 01001 return Elements; 01002 } 01003 01004 Constant *ConstantStruct::get(const StructType *Ty, 01005 const std::vector<Constant*> &V) { 01006 // Create a ConstantAggregateZero value if all elements are zeros... 01007 for (unsigned i = 0, e = V.size(); i != e; ++i) 01008 if (!V[i]->isNullValue()) 01009 return StructConstants.getOrCreate(Ty, V); 01010 01011 return ConstantAggregateZero::get(Ty); 01012 } 01013 01014 Constant *ConstantStruct::get(const std::vector<Constant*> &V) { 01015 std::vector<const Type*> StructEls; 01016 StructEls.reserve(V.size()); 01017 for (unsigned i = 0, e = V.size(); i != e; ++i) 01018 StructEls.push_back(V[i]->getType()); 01019 return get(StructType::get(StructEls), V); 01020 } 01021 01022 // destroyConstant - Remove the constant from the constant table... 01023 // 01024 void ConstantStruct::destroyConstant() { 01025 StructConstants.remove(this); 01026 destroyConstantImpl(); 01027 } 01028 01029 //---- ConstantPacked::get() implementation... 01030 // 01031 namespace llvm { 01032 template<> 01033 struct ConvertConstantType<ConstantPacked, PackedType> { 01034 static void convert(ConstantPacked *OldC, const PackedType *NewTy) { 01035 // Make everyone now use a constant of the new type... 01036 std::vector<Constant*> C; 01037 for (unsigned i = 0, e = OldC->getNumOperands(); i != e; ++i) 01038 C.push_back(cast<Constant>(OldC->getOperand(i))); 01039 Constant *New = ConstantPacked::get(NewTy, C); 01040 assert(New != OldC && "Didn't replace constant??"); 01041 OldC->uncheckedReplaceAllUsesWith(New); 01042 OldC->destroyConstant(); // This constant is now dead, destroy it. 01043 } 01044 }; 01045 } 01046 01047 static std::vector<Constant*> getValType(ConstantPacked *CP) { 01048 std::vector<Constant*> Elements; 01049 Elements.reserve(CP->getNumOperands()); 01050 for (unsigned i = 0, e = CP->getNumOperands(); i != e; ++i) 01051 Elements.push_back(CP->getOperand(i)); 01052 return Elements; 01053 } 01054 01055 static ValueMap<std::vector<Constant*>, PackedType, 01056 ConstantPacked> PackedConstants; 01057 01058 Constant *ConstantPacked::get(const PackedType *Ty, 01059 const std::vector<Constant*> &V) { 01060 // If this is an all-zero packed, return a ConstantAggregateZero object 01061 if (!V.empty()) { 01062 Constant *C = V[0]; 01063 if (!C->isNullValue()) 01064 return PackedConstants.getOrCreate(Ty, V); 01065 for (unsigned i = 1, e = V.size(); i != e; ++i) 01066 if (V[i] != C) 01067 return PackedConstants.getOrCreate(Ty, V); 01068 } 01069 return ConstantAggregateZero::get(Ty); 01070 } 01071 01072 Constant *ConstantPacked::get(const std::vector<Constant*> &V) { 01073 assert(!V.empty() && "Cannot infer type if V is empty"); 01074 return get(PackedType::get(V.front()->getType(),V.size()), V); 01075 } 01076 01077 // destroyConstant - Remove the constant from the constant table... 01078 // 01079 void ConstantPacked::destroyConstant() { 01080 PackedConstants.remove(this); 01081 destroyConstantImpl(); 01082 } 01083 01084 //---- ConstantPointerNull::get() implementation... 01085 // 01086 01087 namespace llvm { 01088 // ConstantPointerNull does not take extra "value" argument... 01089 template<class ValType> 01090 struct ConstantCreator<ConstantPointerNull, PointerType, ValType> { 01091 static ConstantPointerNull *create(const PointerType *Ty, const ValType &V){ 01092 return new ConstantPointerNull(Ty); 01093 } 01094 }; 01095 01096 template<> 01097 struct ConvertConstantType<ConstantPointerNull, PointerType> { 01098 static void convert(ConstantPointerNull *OldC, const PointerType *NewTy) { 01099 // Make everyone now use a constant of the new type... 01100 Constant *New = ConstantPointerNull::get(NewTy); 01101 assert(New != OldC && "Didn't replace constant??"); 01102 OldC->uncheckedReplaceAllUsesWith(New); 01103 OldC->destroyConstant(); // This constant is now dead, destroy it. 01104 } 01105 }; 01106 } 01107 01108 static ValueMap<char, PointerType, ConstantPointerNull> NullPtrConstants; 01109 01110 static char getValType(ConstantPointerNull *) { 01111 return 0; 01112 } 01113 01114 01115 ConstantPointerNull *ConstantPointerNull::get(const PointerType *Ty) { 01116 return NullPtrConstants.getOrCreate(Ty, 0); 01117 } 01118 01119 // destroyConstant - Remove the constant from the constant table... 01120 // 01121 void ConstantPointerNull::destroyConstant() { 01122 NullPtrConstants.remove(this); 01123 destroyConstantImpl(); 01124 } 01125 01126 01127 //---- UndefValue::get() implementation... 01128 // 01129 01130 namespace llvm { 01131 // UndefValue does not take extra "value" argument... 01132 template<class ValType> 01133 struct ConstantCreator<UndefValue, Type, ValType> { 01134 static UndefValue *create(const Type *Ty, const ValType &V) { 01135 return new UndefValue(Ty); 01136 } 01137 }; 01138 01139 template<> 01140 struct ConvertConstantType<UndefValue, Type> { 01141 static void convert(UndefValue *OldC, const Type *NewTy) { 01142 // Make everyone now use a constant of the new type. 01143 Constant *New = UndefValue::get(NewTy); 01144 assert(New != OldC && "Didn't replace constant??"); 01145 OldC->uncheckedReplaceAllUsesWith(New); 01146 OldC->destroyConstant(); // This constant is now dead, destroy it. 01147 } 01148 }; 01149 } 01150 01151 static ValueMap<char, Type, UndefValue> UndefValueConstants; 01152 01153 static char getValType(UndefValue *) { 01154 return 0; 01155 } 01156 01157 01158 UndefValue *UndefValue::get(const Type *Ty) { 01159 return UndefValueConstants.getOrCreate(Ty, 0); 01160 } 01161 01162 // destroyConstant - Remove the constant from the constant table. 01163 // 01164 void UndefValue::destroyConstant() { 01165 UndefValueConstants.remove(this); 01166 destroyConstantImpl(); 01167 } 01168 01169 01170 01171 01172 //---- ConstantExpr::get() implementations... 01173 // 01174 typedef std::pair<unsigned, std::vector<Constant*> > ExprMapKeyType; 01175 01176 namespace llvm { 01177 template<> 01178 struct ConstantCreator<ConstantExpr, Type, ExprMapKeyType> { 01179 static ConstantExpr *create(const Type *Ty, const ExprMapKeyType &V) { 01180 if (V.first == Instruction::Cast) 01181 return new UnaryConstantExpr(Instruction::Cast, V.second[0], Ty); 01182 if ((V.first >= Instruction::BinaryOpsBegin && 01183 V.first < Instruction::BinaryOpsEnd) || 01184 V.first == Instruction::Shl || V.first == Instruction::Shr) 01185 return new BinaryConstantExpr(V.first, V.second[0], V.second[1]); 01186 if (V.first == Instruction::Select) 01187 return new SelectConstantExpr(V.second[0], V.second[1], V.second[2]); 01188 if (V.first == Instruction::ExtractElement) 01189 return new ExtractElementConstantExpr(V.second[0], V.second[1]); 01190 if (V.first == Instruction::InsertElement) 01191 return new InsertElementConstantExpr(V.second[0], V.second[1], 01192 V.second[2]); 01193 if (V.first == Instruction::ShuffleVector) 01194 return new ShuffleVectorConstantExpr(V.second[0], V.second[1], 01195 V.second[2]); 01196 01197 assert(V.first == Instruction::GetElementPtr && "Invalid ConstantExpr!"); 01198 01199 std::vector<Constant*> IdxList(V.second.begin()+1, V.second.end()); 01200 return new GetElementPtrConstantExpr(V.second[0], IdxList, Ty); 01201 } 01202 }; 01203 01204 template<> 01205 struct ConvertConstantType<ConstantExpr, Type> { 01206 static void convert(ConstantExpr *OldC, const Type *NewTy) { 01207 Constant *New; 01208 switch (OldC->getOpcode()) { 01209 case Instruction::Cast: 01210 New = ConstantExpr::getCast(OldC->getOperand(0), NewTy); 01211 break; 01212 case Instruction::Select: 01213 New = ConstantExpr::getSelectTy(NewTy, OldC->getOperand(0), 01214 OldC->getOperand(1), 01215 OldC->getOperand(2)); 01216 break; 01217 case Instruction::Shl: 01218 case Instruction::Shr: 01219 New = ConstantExpr::getShiftTy(NewTy, OldC->getOpcode(), 01220 OldC->getOperand(0), OldC->getOperand(1)); 01221 break; 01222 default: 01223 assert(OldC->getOpcode() >= Instruction::BinaryOpsBegin && 01224 OldC->getOpcode() < Instruction::BinaryOpsEnd); 01225 New = ConstantExpr::getTy(NewTy, OldC->getOpcode(), OldC->getOperand(0), 01226 OldC->getOperand(1)); 01227 break; 01228 case Instruction::GetElementPtr: 01229 // Make everyone now use a constant of the new type... 01230 std::vector<Value*> Idx(OldC->op_begin()+1, OldC->op_end()); 01231 New = ConstantExpr::getGetElementPtrTy(NewTy, OldC->getOperand(0), Idx); 01232 break; 01233 } 01234 01235 assert(New != OldC && "Didn't replace constant??"); 01236 OldC->uncheckedReplaceAllUsesWith(New); 01237 OldC->destroyConstant(); // This constant is now dead, destroy it. 01238 } 01239 }; 01240 } // end namespace llvm 01241 01242 01243 static ExprMapKeyType getValType(ConstantExpr *CE) { 01244 std::vector<Constant*> Operands; 01245 Operands.reserve(CE->getNumOperands()); 01246 for (unsigned i = 0, e = CE->getNumOperands(); i != e; ++i) 01247 Operands.push_back(cast<Constant>(CE->getOperand(i))); 01248 return ExprMapKeyType(CE->getOpcode(), Operands); 01249 } 01250 01251 static ValueMap<ExprMapKeyType, Type, ConstantExpr> ExprConstants; 01252 01253 Constant *ConstantExpr::getCast(Constant *C, const Type *Ty) { 01254 assert(Ty->isFirstClassType() && "Cannot cast to an aggregate type!"); 01255 01256 if (Constant *FC = ConstantFoldCastInstruction(C, Ty)) 01257 return FC; // Fold a few common cases... 01258 01259 // Look up the constant in the table first to ensure uniqueness 01260 std::vector<Constant*> argVec(1, C); 01261 ExprMapKeyType Key = std::make_pair(Instruction::Cast, argVec); 01262 return ExprConstants.getOrCreate(Ty, Key); 01263 } 01264 01265 Constant *ConstantExpr::getSignExtend(Constant *C, const Type *Ty) { 01266 assert(C->getType()->isIntegral() && Ty->isIntegral() && 01267 C->getType()->getPrimitiveSize() <= Ty->getPrimitiveSize() && 01268 "This is an illegal sign extension!"); 01269 if (C->getType() != Type::BoolTy) { 01270 C = ConstantExpr::getCast(C, C->getType()->getSignedVersion()); 01271 return ConstantExpr::getCast(C, Ty); 01272 } else { 01273 if (C == ConstantBool::True) 01274 return ConstantIntegral::getAllOnesValue(Ty); 01275 else 01276 return ConstantIntegral::getNullValue(Ty); 01277 } 01278 } 01279 01280 Constant *ConstantExpr::getZeroExtend(Constant *C, const Type *Ty) { 01281 assert(C->getType()->isIntegral() && Ty->isIntegral() && 01282 C->getType()->getPrimitiveSize() <= Ty->getPrimitiveSize() && 01283 "This is an illegal zero extension!"); 01284 if (C->getType() != Type::BoolTy) 01285 C = ConstantExpr::getCast(C, C->getType()->getUnsignedVersion()); 01286 return ConstantExpr::getCast(C, Ty); 01287 } 01288 01289 Constant *ConstantExpr::getSizeOf(const Type *Ty) { 01290 // sizeof is implemented as: (ulong) gep (Ty*)null, 1 01291 return getCast( 01292 getGetElementPtr(getNullValue(PointerType::get(Ty)), 01293 std::vector<Constant*>(1, ConstantInt::get(Type::UIntTy, 1))), 01294 Type::ULongTy); 01295 } 01296 01297 Constant *ConstantExpr::getPtrPtrFromArrayPtr(Constant *C) { 01298 // pointer from array is implemented as: getelementptr arr ptr, 0, 0 01299 static std::vector<Constant*> Indices(2, ConstantUInt::get(Type::UIntTy, 0)); 01300 01301 return ConstantExpr::getGetElementPtr(C, Indices); 01302 } 01303 01304 Constant *ConstantExpr::getTy(const Type *ReqTy, unsigned Opcode, 01305 Constant *C1, Constant *C2) { 01306 if (Opcode == Instruction::Shl || Opcode == Instruction::Shr) 01307 return getShiftTy(ReqTy, Opcode, C1, C2); 01308 // Check the operands for consistency first 01309 assert((Opcode >= Instruction::BinaryOpsBegin && 01310 Opcode < Instruction::BinaryOpsEnd) && 01311 "Invalid opcode in binary constant expression"); 01312 assert(C1->getType() == C2->getType() && 01313 "Operand types in binary constant expression should match"); 01314 01315 if (ReqTy == C1->getType() || (Instruction::isRelational(Opcode) && 01316 ReqTy == Type::BoolTy)) 01317 if (Constant *FC = ConstantFoldBinaryInstruction(Opcode, C1, C2)) 01318 return FC; // Fold a few common cases... 01319 01320 std::vector<Constant*> argVec(1, C1); argVec.push_back(C2); 01321 ExprMapKeyType Key = std::make_pair(Opcode, argVec); 01322 return ExprConstants.getOrCreate(ReqTy, Key); 01323 } 01324 01325 Constant *ConstantExpr::get(unsigned Opcode, Constant *C1, Constant *C2) { 01326 #ifndef NDEBUG 01327 switch (Opcode) { 01328 case Instruction::Add: case Instruction::Sub: 01329 case Instruction::Mul: case Instruction::Div: 01330 case Instruction::Rem: 01331 assert(C1->getType() == C2->getType() && "Op types should be identical!"); 01332 assert((C1->getType()->isInteger() || C1->getType()->isFloatingPoint() || 01333 isa<PackedType>(C1->getType())) && 01334 "Tried to create an arithmetic operation on a non-arithmetic type!"); 01335 break; 01336 case Instruction::And: 01337 case Instruction::Or: 01338 case Instruction::Xor: 01339 assert(C1->getType() == C2->getType() && "Op types should be identical!"); 01340 assert((C1->getType()->isIntegral() || isa<PackedType>(C1->getType())) && 01341 "Tried to create a logical operation on a non-integral type!"); 01342 break; 01343 case Instruction::SetLT: case Instruction::SetGT: case Instruction::SetLE: 01344 case Instruction::SetGE: case Instruction::SetEQ: case Instruction::SetNE: 01345 assert(C1->getType() == C2->getType() && "Op types should be identical!"); 01346 break; 01347 case Instruction::Shl: 01348 case Instruction::Shr: 01349 assert(C2->getType() == Type::UByteTy && "Shift should be by ubyte!"); 01350 assert((C1->getType()->isInteger() || isa<PackedType>(C1->getType())) && 01351 "Tried to create a shift operation on a non-integer type!"); 01352 break; 01353 default: 01354 break; 01355 } 01356 #endif 01357 01358 if (Instruction::isRelational(Opcode)) 01359 return getTy(Type::BoolTy, Opcode, C1, C2); 01360 else 01361 return getTy(C1->getType(), Opcode, C1, C2); 01362 } 01363 01364 Constant *ConstantExpr::getSelectTy(const Type *ReqTy, Constant *C, 01365 Constant *V1, Constant *V2) { 01366 assert(C->getType() == Type::BoolTy && "Select condition must be bool!"); 01367 assert(V1->getType() == V2->getType() && "Select value types must match!"); 01368 assert(V1->getType()->isFirstClassType() && "Cannot select aggregate type!"); 01369 01370 if (ReqTy == V1->getType()) 01371 if (Constant *SC = ConstantFoldSelectInstruction(C, V1, V2)) 01372 return SC; // Fold common cases 01373 01374 std::vector<Constant*> argVec(3, C); 01375 argVec[1] = V1; 01376 argVec[2] = V2; 01377 ExprMapKeyType Key = std::make_pair(Instruction::Select, argVec); 01378 return ExprConstants.getOrCreate(ReqTy, Key); 01379 } 01380 01381 /// getShiftTy - Return a shift left or shift right constant expr 01382 Constant *ConstantExpr::getShiftTy(const Type *ReqTy, unsigned Opcode, 01383 Constant *C1, Constant *C2) { 01384 // Check the operands for consistency first 01385 assert((Opcode == Instruction::Shl || 01386 Opcode == Instruction::Shr) && 01387 "Invalid opcode in binary constant expression"); 01388 assert(C1->getType()->isIntegral() && C2->getType() == Type::UByteTy && 01389 "Invalid operand types for Shift constant expr!"); 01390 01391 if (Constant *FC = ConstantFoldBinaryInstruction(Opcode, C1, C2)) 01392 return FC; // Fold a few common cases... 01393 01394 // Look up the constant in the table first to ensure uniqueness 01395 std::vector<Constant*> argVec(1, C1); argVec.push_back(C2); 01396 ExprMapKeyType Key = std::make_pair(Opcode, argVec); 01397 return ExprConstants.getOrCreate(ReqTy, Key); 01398 } 01399 01400 01401 Constant *ConstantExpr::getGetElementPtrTy(const Type *ReqTy, Constant *C, 01402 const std::vector<Value*> &IdxList) { 01403 assert(GetElementPtrInst::getIndexedType(C->getType(), IdxList, true) && 01404 "GEP indices invalid!"); 01405 01406 if (Constant *FC = ConstantFoldGetElementPtr(C, IdxList)) 01407 return FC; // Fold a few common cases... 01408 01409 assert(isa<PointerType>(C->getType()) && 01410 "Non-pointer type for constant GetElementPtr expression"); 01411 // Look up the constant in the table first to ensure uniqueness 01412 std::vector<Constant*> ArgVec; 01413 ArgVec.reserve(IdxList.size()+1); 01414 ArgVec.push_back(C); 01415 for (unsigned i = 0, e = IdxList.size(); i != e; ++i) 01416 ArgVec.push_back(cast<Constant>(IdxList[i])); 01417 const ExprMapKeyType &Key = std::make_pair(Instruction::GetElementPtr,ArgVec); 01418 return ExprConstants.getOrCreate(ReqTy, Key); 01419 } 01420 01421 Constant *ConstantExpr::getGetElementPtr(Constant *C, 01422 const std::vector<Constant*> &IdxList){ 01423 // Get the result type of the getelementptr! 01424 std::vector<Value*> VIdxList(IdxList.begin(), IdxList.end()); 01425 01426 const Type *Ty = GetElementPtrInst::getIndexedType(C->getType(), VIdxList, 01427 true); 01428 assert(Ty && "GEP indices invalid!"); 01429 return getGetElementPtrTy(PointerType::get(Ty), C, VIdxList); 01430 } 01431 01432 Constant *ConstantExpr::getGetElementPtr(Constant *C, 01433 const std::vector<Value*> &IdxList) { 01434 // Get the result type of the getelementptr! 01435 const Type *Ty = GetElementPtrInst::getIndexedType(C->getType(), IdxList, 01436 true); 01437 assert(Ty && "GEP indices invalid!"); 01438 return getGetElementPtrTy(PointerType::get(Ty), C, IdxList); 01439 } 01440 01441 Constant *ConstantExpr::getExtractElementTy(const Type *ReqTy, Constant *Val, 01442 Constant *Idx) { 01443 if (Constant *FC = ConstantFoldExtractElementInstruction(Val, Idx)) 01444 return FC; // Fold a few common cases... 01445 // Look up the constant in the table first to ensure uniqueness 01446 std::vector<Constant*> ArgVec(1, Val); 01447 ArgVec.push_back(Idx); 01448 const ExprMapKeyType &Key = std::make_pair(Instruction::ExtractElement,ArgVec); 01449 return ExprConstants.getOrCreate(ReqTy, Key); 01450 } 01451 01452 Constant *ConstantExpr::getExtractElement(Constant *Val, Constant *Idx) { 01453 assert(isa<PackedType>(Val->getType()) && 01454 "Tried to create extractelement operation on non-packed type!"); 01455 assert(Idx->getType() == Type::UIntTy && 01456 "Extractelement index must be uint type!"); 01457 return getExtractElementTy(cast<PackedType>(Val->getType())->getElementType(), 01458 Val, Idx); 01459 } 01460 01461 Constant *ConstantExpr::getInsertElementTy(const Type *ReqTy, Constant *Val, 01462 Constant *Elt, Constant *Idx) { 01463 if (Constant *FC = ConstantFoldInsertElementInstruction(Val, Elt, Idx)) 01464 return FC; // Fold a few common cases... 01465 // Look up the constant in the table first to ensure uniqueness 01466 std::vector<Constant*> ArgVec(1, Val); 01467 ArgVec.push_back(Elt); 01468 ArgVec.push_back(Idx); 01469 const ExprMapKeyType &Key = std::make_pair(Instruction::InsertElement,ArgVec); 01470 return ExprConstants.getOrCreate(ReqTy, Key); 01471 } 01472 01473 Constant *ConstantExpr::getInsertElement(Constant *Val, Constant *Elt, 01474 Constant *Idx) { 01475 assert(isa<PackedType>(Val->getType()) && 01476 "Tried to create insertelement operation on non-packed type!"); 01477 assert(Elt->getType() == cast<PackedType>(Val->getType())->getElementType() 01478 && "Insertelement types must match!"); 01479 assert(Idx->getType() == Type::UIntTy && 01480 "Insertelement index must be uint type!"); 01481 return getInsertElementTy(cast<PackedType>(Val->getType())->getElementType(), 01482 Val, Elt, Idx); 01483 } 01484 01485 Constant *ConstantExpr::getShuffleVectorTy(const Type *ReqTy, Constant *V1, 01486 Constant *V2, Constant *Mask) { 01487 if (Constant *FC = ConstantFoldShuffleVectorInstruction(V1, V2, Mask)) 01488 return FC; // Fold a few common cases... 01489 // Look up the constant in the table first to ensure uniqueness 01490 std::vector<Constant*> ArgVec(1, V1); 01491 ArgVec.push_back(V2); 01492 ArgVec.push_back(Mask); 01493 const ExprMapKeyType &Key = std::make_pair(Instruction::ShuffleVector,ArgVec); 01494 return ExprConstants.getOrCreate(ReqTy, Key); 01495 } 01496 01497 Constant *ConstantExpr::getShuffleVector(Constant *V1, Constant *V2, 01498 Constant *Mask) { 01499 assert(ShuffleVectorInst::isValidOperands(V1, V2, Mask) && 01500 "Invalid shuffle vector constant expr operands!"); 01501 return getShuffleVectorTy(V1->getType(), V1, V2, Mask); 01502 } 01503 01504 01505 // destroyConstant - Remove the constant from the constant table... 01506 // 01507 void ConstantExpr::destroyConstant() { 01508 ExprConstants.remove(this); 01509 destroyConstantImpl(); 01510 } 01511 01512 const char *ConstantExpr::getOpcodeName() const { 01513 return Instruction::getOpcodeName(getOpcode()); 01514 } 01515 01516 //===----------------------------------------------------------------------===// 01517 // replaceUsesOfWithOnConstant implementations 01518 01519 void ConstantArray::replaceUsesOfWithOnConstant(Value *From, Value *To, 01520 Use *U) { 01521 assert(isa<Constant>(To) && "Cannot make Constant refer to non-constant!"); 01522 Constant *ToC = cast<Constant>(To); 01523 01524 unsigned OperandToUpdate = U-OperandList; 01525 assert(getOperand(OperandToUpdate) == From && "ReplaceAllUsesWith broken!"); 01526 01527 std::pair<ArrayConstantsTy::MapKey, ConstantArray*> Lookup; 01528 Lookup.first.first = getType(); 01529 Lookup.second = this; 01530 01531 std::vector<Constant*> &Values = Lookup.first.second; 01532 Values.reserve(getNumOperands()); // Build replacement array. 01533 01534 // Fill values with the modified operands of the constant array. Also, 01535 // compute whether this turns into an all-zeros array. 01536 bool isAllZeros = false; 01537 if (!ToC->isNullValue()) { 01538 for (Use *O = OperandList, *E = OperandList+getNumOperands(); O != E; ++O) 01539 Values.push_back(cast<Constant>(O->get())); 01540 } else { 01541 isAllZeros = true; 01542 for (Use *O = OperandList, *E = OperandList+getNumOperands(); O != E; ++O) { 01543 Constant *Val = cast<Constant>(O->get()); 01544 Values.push_back(Val); 01545 if (isAllZeros) isAllZeros = Val->isNullValue(); 01546 } 01547 } 01548 Values[OperandToUpdate] = ToC; 01549 01550 Constant *Replacement = 0; 01551 if (isAllZeros) { 01552 Replacement = ConstantAggregateZero::get(getType()); 01553 } else { 01554 // Check to see if we have this array type already. 01555 bool Exists; 01556 ArrayConstantsTy::MapIterator I = 01557 ArrayConstants.InsertOrGetItem(Lookup, Exists); 01558 01559 if (Exists) { 01560 Replacement = I->second; 01561 } else { 01562 // Okay, the new shape doesn't exist in the system yet. Instead of 01563 // creating a new constant array, inserting it, replaceallusesof'ing the 01564 // old with the new, then deleting the old... just update the current one 01565 // in place! 01566 ArrayConstants.MoveConstantToNewSlot(this, I); 01567 01568 // Update to the new value. 01569 setOperand(OperandToUpdate, ToC); 01570 return; 01571 } 01572 } 01573 01574 // Otherwise, I do need to replace this with an existing value. 01575 assert(Replacement != this && "I didn't contain From!"); 01576 01577 // Everyone using this now uses the replacement. 01578 uncheckedReplaceAllUsesWith(Replacement); 01579 01580 // Delete the old constant! 01581 destroyConstant(); 01582 } 01583 01584 void ConstantStruct::replaceUsesOfWithOnConstant(Value *From, Value *To, 01585 Use *U) { 01586 assert(isa<Constant>(To) && "Cannot make Constant refer to non-constant!"); 01587 Constant *ToC = cast<Constant>(To); 01588 01589 unsigned OperandToUpdate = U-OperandList; 01590 assert(getOperand(OperandToUpdate) == From && "ReplaceAllUsesWith broken!"); 01591 01592 std::pair<StructConstantsTy::MapKey, ConstantStruct*> Lookup; 01593 Lookup.first.first = getType(); 01594 Lookup.second = this; 01595 std::vector<Constant*> &Values = Lookup.first.second; 01596 Values.reserve(getNumOperands()); // Build replacement struct. 01597 01598 01599 // Fill values with the modified operands of the constant struct. Also, 01600 // compute whether this turns into an all-zeros struct. 01601 bool isAllZeros = false; 01602 if (!ToC->isNullValue()) { 01603 for (Use *O = OperandList, *E = OperandList+getNumOperands(); O != E; ++O) 01604 Values.push_back(cast<Constant>(O->get())); 01605 } else { 01606 isAllZeros = true; 01607 for (Use *O = OperandList, *E = OperandList+getNumOperands(); O != E; ++O) { 01608 Constant *Val = cast<Constant>(O->get()); 01609 Values.push_back(Val); 01610 if (isAllZeros) isAllZeros = Val->isNullValue(); 01611 } 01612 } 01613 Values[OperandToUpdate] = ToC; 01614 01615 Constant *Replacement = 0; 01616 if (isAllZeros) { 01617 Replacement = ConstantAggregateZero::get(getType()); 01618 } else { 01619 // Check to see if we have this array type already. 01620 bool Exists; 01621 StructConstantsTy::MapIterator I = 01622 StructConstants.InsertOrGetItem(Lookup, Exists); 01623 01624 if (Exists) { 01625 Replacement = I->second; 01626 } else { 01627 // Okay, the new shape doesn't exist in the system yet. Instead of 01628 // creating a new constant struct, inserting it, replaceallusesof'ing the 01629 // old with the new, then deleting the old... just update the current one 01630 // in place! 01631 StructConstants.MoveConstantToNewSlot(this, I); 01632 01633 // Update to the new value. 01634 setOperand(OperandToUpdate, ToC); 01635 return; 01636 } 01637 } 01638 01639 assert(Replacement != this && "I didn't contain From!"); 01640 01641 // Everyone using this now uses the replacement. 01642 uncheckedReplaceAllUsesWith(Replacement); 01643 01644 // Delete the old constant! 01645 destroyConstant(); 01646 } 01647 01648 void ConstantPacked::replaceUsesOfWithOnConstant(Value *From, Value *To, 01649 Use *U) { 01650 assert(isa<Constant>(To) && "Cannot make Constant refer to non-constant!"); 01651 01652 std::vector<Constant*> Values; 01653 Values.reserve(getNumOperands()); // Build replacement array... 01654 for (unsigned i = 0, e = getNumOperands(); i != e; ++i) { 01655 Constant *Val = getOperand(i); 01656 if (Val == From) Val = cast<Constant>(To); 01657 Values.push_back(Val); 01658 } 01659 01660 Constant *Replacement = ConstantPacked::get(getType(), Values); 01661 assert(Replacement != this && "I didn't contain From!"); 01662 01663 // Everyone using this now uses the replacement. 01664 uncheckedReplaceAllUsesWith(Replacement); 01665 01666 // Delete the old constant! 01667 destroyConstant(); 01668 } 01669 01670 void ConstantExpr::replaceUsesOfWithOnConstant(Value *From, Value *ToV, 01671 Use *U) { 01672 assert(isa<Constant>(ToV) && "Cannot make Constant refer to non-constant!"); 01673 Constant *To = cast<Constant>(ToV); 01674 01675 Constant *Replacement = 0; 01676 if (getOpcode() == Instruction::GetElementPtr) { 01677 std::vector<Constant*> Indices; 01678 Constant *Pointer = getOperand(0); 01679 Indices.reserve(getNumOperands()-1); 01680 if (Pointer == From) Pointer = To; 01681 01682 for (unsigned i = 1, e = getNumOperands(); i != e; ++i) { 01683 Constant *Val = getOperand(i); 01684 if (Val == From) Val = To; 01685 Indices.push_back(Val); 01686 } 01687 Replacement = ConstantExpr::getGetElementPtr(Pointer, Indices); 01688 } else if (getOpcode() == Instruction::Cast) { 01689 assert(getOperand(0) == From && "Cast only has one use!"); 01690 Replacement = ConstantExpr::getCast(To, getType()); 01691 } else if (getOpcode() == Instruction::Select) { 01692 Constant *C1 = getOperand(0); 01693 Constant *C2 = getOperand(1); 01694 Constant *C3 = getOperand(2); 01695 if (C1 == From) C1 = To; 01696 if (C2 == From) C2 = To; 01697 if (C3 == From) C3 = To; 01698 Replacement = ConstantExpr::getSelect(C1, C2, C3); 01699 } else if (getOpcode() == Instruction::ExtractElement) { 01700 Constant *C1 = getOperand(0); 01701 Constant *C2 = getOperand(1); 01702 if (C1 == From) C1 = To; 01703 if (C2 == From) C2 = To; 01704 Replacement = ConstantExpr::getExtractElement(C1, C2); 01705 } else if (getOpcode() == Instruction::InsertElement) { 01706 Constant *C1 = getOperand(0); 01707 Constant *C2 = getOperand(1); 01708 Constant *C3 = getOperand(1); 01709 if (C1 == From) C1 = To; 01710 if (C2 == From) C2 = To; 01711 if (C3 == From) C3 = To; 01712 Replacement = ConstantExpr::getInsertElement(C1, C2, C3); 01713 } else if (getOpcode() == Instruction::ShuffleVector) { 01714 Constant *C1 = getOperand(0); 01715 Constant *C2 = getOperand(1); 01716 Constant *C3 = getOperand(2); 01717 if (C1 == From) C1 = To; 01718 if (C2 == From) C2 = To; 01719 if (C3 == From) C3 = To; 01720 Replacement = ConstantExpr::getShuffleVector(C1, C2, C3); 01721 } else if (getNumOperands() == 2) { 01722 Constant *C1 = getOperand(0); 01723 Constant *C2 = getOperand(1); 01724 if (C1 == From) C1 = To; 01725 if (C2 == From) C2 = To; 01726 Replacement = ConstantExpr::get(getOpcode(), C1, C2); 01727 } else { 01728 assert(0 && "Unknown ConstantExpr type!"); 01729 return; 01730 } 01731 01732 assert(Replacement != this && "I didn't contain From!"); 01733 01734 // Everyone using this now uses the replacement. 01735 uncheckedReplaceAllUsesWith(Replacement); 01736 01737 // Delete the old constant! 01738 destroyConstant(); 01739 } 01740 01741 01742 01743 /// clearAllValueMaps - This method frees all internal memory used by the 01744 /// constant subsystem, which can be used in environments where this memory 01745 /// is otherwise reported as a leak. 01746 void Constant::clearAllValueMaps() { 01747 std::vector<Constant *> Constants; 01748 01749 DoubleConstants.clear(Constants); 01750 FloatConstants.clear(Constants); 01751 SIntConstants.clear(Constants); 01752 UIntConstants.clear(Constants); 01753 AggZeroConstants.clear(Constants); 01754 ArrayConstants.clear(Constants); 01755 StructConstants.clear(Constants); 01756 PackedConstants.clear(Constants); 01757 NullPtrConstants.clear(Constants); 01758 UndefValueConstants.clear(Constants); 01759 ExprConstants.clear(Constants); 01760 01761 for (std::vector<Constant *>::iterator I = Constants.begin(), 01762 E = Constants.end(); I != E; ++I) 01763 (*I)->dropAllReferences(); 01764 for (std::vector<Constant *>::iterator I = Constants.begin(), 01765 E = Constants.end(); I != E; ++I) 01766 (*I)->destroyConstantImpl(); 01767 Constants.clear(); 01768 } 01769 01770 /// getStringValue - Turn an LLVM constant pointer that eventually points to a 01771 /// global into a string value. Return an empty string if we can't do it. 01772 /// Parameter Chop determines if the result is chopped at the first null 01773 /// terminator. 01774 /// 01775 std::string Constant::getStringValue(bool Chop, unsigned Offset) { 01776 if (GlobalVariable *GV = dyn_cast<GlobalVariable>(this)) { 01777 if (GV->hasInitializer() && isa<ConstantArray>(GV->getInitializer())) { 01778 ConstantArray *Init = cast<ConstantArray>(GV->getInitializer()); 01779 if (Init->isString()) { 01780 std::string Result = Init->getAsString(); 01781 if (Offset < Result.size()) { 01782 // If we are pointing INTO The string, erase the beginning... 01783 Result.erase(Result.begin(), Result.begin()+Offset); 01784 01785 // Take off the null terminator, and any string fragments after it. 01786 if (Chop) { 01787 std::string::size_type NullPos = Result.find_first_of((char)0); 01788 if (NullPos != std::string::npos) 01789 Result.erase(Result.begin()+NullPos, Result.end()); 01790 } 01791 return Result; 01792 } 01793 } 01794 } 01795 } else if (Constant *C = dyn_cast<Constant>(this)) { 01796 if (GlobalValue *GV = dyn_cast<GlobalValue>(C)) 01797 return GV->getStringValue(Chop, Offset); 01798 else if (ConstantExpr *CE = dyn_cast<ConstantExpr>(C)) { 01799 if (CE->getOpcode() == Instruction::GetElementPtr) { 01800 // Turn a gep into the specified offset. 01801 if (CE->getNumOperands() == 3 && 01802 cast<Constant>(CE->getOperand(1))->isNullValue() && 01803 isa<ConstantInt>(CE->getOperand(2))) { 01804 Offset += cast<ConstantInt>(CE->getOperand(2))->getRawValue(); 01805 return CE->getOperand(0)->getStringValue(Chop, Offset); 01806 } 01807 } 01808 } 01809 } 01810 return ""; 01811 } 01812