LLVM API Documentation
00001 //===-- Constants.cpp - Implement Constant nodes --------------------------===// 00002 // 00003 // The LLVM Compiler Infrastructure 00004 // 00005 // This file is distributed under the University of Illinois Open Source 00006 // 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 "ConstantFold.h" 00016 #include "llvm/DerivedTypes.h" 00017 #include "llvm/GlobalValue.h" 00018 #include "llvm/Instructions.h" 00019 #include "llvm/Module.h" 00020 #include "llvm/ADT/StringExtras.h" 00021 #include "llvm/Support/Compiler.h" 00022 #include "llvm/Support/Debug.h" 00023 #include "llvm/Support/ManagedStatic.h" 00024 #include "llvm/Support/MathExtras.h" 00025 #include "llvm/ADT/DenseMap.h" 00026 #include "llvm/ADT/SmallVector.h" 00027 #include <algorithm> 00028 #include <map> 00029 using namespace llvm; 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 DOUT << "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 /// canTrap - Return true if evaluation of this constant could trap. This is 00064 /// true for things like constant expressions that could divide by zero. 00065 bool Constant::canTrap() const { 00066 assert(getType()->isFirstClassType() && "Cannot evaluate aggregate vals!"); 00067 // The only thing that could possibly trap are constant exprs. 00068 const ConstantExpr *CE = dyn_cast<ConstantExpr>(this); 00069 if (!CE) return false; 00070 00071 // ConstantExpr traps if any operands can trap. 00072 for (unsigned i = 0, e = getNumOperands(); i != e; ++i) 00073 if (getOperand(i)->canTrap()) 00074 return true; 00075 00076 // Otherwise, only specific operations can trap. 00077 switch (CE->getOpcode()) { 00078 default: 00079 return false; 00080 case Instruction::UDiv: 00081 case Instruction::SDiv: 00082 case Instruction::FDiv: 00083 case Instruction::URem: 00084 case Instruction::SRem: 00085 case Instruction::FRem: 00086 // Div and rem can trap if the RHS is not known to be non-zero. 00087 if (!isa<ConstantInt>(getOperand(1)) || getOperand(1)->isNullValue()) 00088 return true; 00089 return false; 00090 } 00091 } 00092 00093 /// ContaintsRelocations - Return true if the constant value contains 00094 /// relocations which cannot be resolved at compile time. 00095 bool Constant::ContainsRelocations() const { 00096 if (isa<GlobalValue>(this)) 00097 return true; 00098 for (unsigned i = 0, e = getNumOperands(); i != e; ++i) 00099 if (getOperand(i)->ContainsRelocations()) 00100 return true; 00101 return false; 00102 } 00103 00104 // Static constructor to create a '0' constant of arbitrary type... 00105 Constant *Constant::getNullValue(const Type *Ty) { 00106 static uint64_t zero[2] = {0, 0}; 00107 switch (Ty->getTypeID()) { 00108 case Type::IntegerTyID: 00109 return ConstantInt::get(Ty, 0); 00110 case Type::FloatTyID: 00111 return ConstantFP::get(APFloat(APInt(32, 0))); 00112 case Type::DoubleTyID: 00113 return ConstantFP::get(APFloat(APInt(64, 0))); 00114 case Type::X86_FP80TyID: 00115 return ConstantFP::get(APFloat(APInt(80, 2, zero))); 00116 case Type::FP128TyID: 00117 return ConstantFP::get(APFloat(APInt(128, 2, zero), true)); 00118 case Type::PPC_FP128TyID: 00119 return ConstantFP::get(APFloat(APInt(128, 2, zero))); 00120 case Type::PointerTyID: 00121 return ConstantPointerNull::get(cast<PointerType>(Ty)); 00122 case Type::StructTyID: 00123 case Type::ArrayTyID: 00124 case Type::VectorTyID: 00125 return ConstantAggregateZero::get(Ty); 00126 default: 00127 // Function, Label, or Opaque type? 00128 assert(!"Cannot create a null constant of that type!"); 00129 return 0; 00130 } 00131 } 00132 00133 Constant *Constant::getAllOnesValue(const Type *Ty) { 00134 if (const IntegerType* ITy = dyn_cast<IntegerType>(Ty)) 00135 return ConstantInt::get(APInt::getAllOnesValue(ITy->getBitWidth())); 00136 return ConstantVector::getAllOnesValue(cast<VectorType>(Ty)); 00137 } 00138 00139 // Static constructor to create an integral constant with all bits set 00140 ConstantInt *ConstantInt::getAllOnesValue(const Type *Ty) { 00141 if (const IntegerType* ITy = dyn_cast<IntegerType>(Ty)) 00142 return ConstantInt::get(APInt::getAllOnesValue(ITy->getBitWidth())); 00143 return 0; 00144 } 00145 00146 /// @returns the value for a vector integer constant of the given type that 00147 /// has all its bits set to true. 00148 /// @brief Get the all ones value 00149 ConstantVector *ConstantVector::getAllOnesValue(const VectorType *Ty) { 00150 std::vector<Constant*> Elts; 00151 Elts.resize(Ty->getNumElements(), 00152 ConstantInt::getAllOnesValue(Ty->getElementType())); 00153 assert(Elts[0] && "Not a vector integer type!"); 00154 return cast<ConstantVector>(ConstantVector::get(Elts)); 00155 } 00156 00157 00158 /// getVectorElements - This method, which is only valid on constant of vector 00159 /// type, returns the elements of the vector in the specified smallvector. 00160 /// This handles breaking down a vector undef into undef elements, etc. For 00161 /// constant exprs and other cases we can't handle, we return an empty vector. 00162 void Constant::getVectorElements(SmallVectorImpl<Constant*> &Elts) const { 00163 assert(isa<VectorType>(getType()) && "Not a vector constant!"); 00164 00165 if (const ConstantVector *CV = dyn_cast<ConstantVector>(this)) { 00166 for (unsigned i = 0, e = CV->getNumOperands(); i != e; ++i) 00167 Elts.push_back(CV->getOperand(i)); 00168 return; 00169 } 00170 00171 const VectorType *VT = cast<VectorType>(getType()); 00172 if (isa<ConstantAggregateZero>(this)) { 00173 Elts.assign(VT->getNumElements(), 00174 Constant::getNullValue(VT->getElementType())); 00175 return; 00176 } 00177 00178 if (isa<UndefValue>(this)) { 00179 Elts.assign(VT->getNumElements(), UndefValue::get(VT->getElementType())); 00180 return; 00181 } 00182 00183 // Unknown type, must be constant expr etc. 00184 } 00185 00186 00187 00188 //===----------------------------------------------------------------------===// 00189 // ConstantInt 00190 //===----------------------------------------------------------------------===// 00191 00192 ConstantInt::ConstantInt(const IntegerType *Ty, const APInt& V) 00193 : Constant(Ty, ConstantIntVal, 0, 0), Val(V) { 00194 assert(V.getBitWidth() == Ty->getBitWidth() && "Invalid constant for type"); 00195 } 00196 00197 ConstantInt *ConstantInt::TheTrueVal = 0; 00198 ConstantInt *ConstantInt::TheFalseVal = 0; 00199 00200 namespace llvm { 00201 void CleanupTrueFalse(void *) { 00202 ConstantInt::ResetTrueFalse(); 00203 } 00204 } 00205 00206 static ManagedCleanup<llvm::CleanupTrueFalse> TrueFalseCleanup; 00207 00208 ConstantInt *ConstantInt::CreateTrueFalseVals(bool WhichOne) { 00209 assert(TheTrueVal == 0 && TheFalseVal == 0); 00210 TheTrueVal = get(Type::Int1Ty, 1); 00211 TheFalseVal = get(Type::Int1Ty, 0); 00212 00213 // Ensure that llvm_shutdown nulls out TheTrueVal/TheFalseVal. 00214 TrueFalseCleanup.Register(); 00215 00216 return WhichOne ? TheTrueVal : TheFalseVal; 00217 } 00218 00219 00220 namespace { 00221 struct DenseMapAPIntKeyInfo { 00222 struct KeyTy { 00223 APInt val; 00224 const Type* type; 00225 KeyTy(const APInt& V, const Type* Ty) : val(V), type(Ty) {} 00226 KeyTy(const KeyTy& that) : val(that.val), type(that.type) {} 00227 bool operator==(const KeyTy& that) const { 00228 return type == that.type && this->val == that.val; 00229 } 00230 bool operator!=(const KeyTy& that) const { 00231 return !this->operator==(that); 00232 } 00233 }; 00234 static inline KeyTy getEmptyKey() { return KeyTy(APInt(1,0), 0); } 00235 static inline KeyTy getTombstoneKey() { return KeyTy(APInt(1,1), 0); } 00236 static unsigned getHashValue(const KeyTy &Key) { 00237 return DenseMapInfo<void*>::getHashValue(Key.type) ^ 00238 Key.val.getHashValue(); 00239 } 00240 static bool isEqual(const KeyTy &LHS, const KeyTy &RHS) { 00241 return LHS == RHS; 00242 } 00243 static bool isPod() { return false; } 00244 }; 00245 } 00246 00247 00248 typedef DenseMap<DenseMapAPIntKeyInfo::KeyTy, ConstantInt*, 00249 DenseMapAPIntKeyInfo> IntMapTy; 00250 static ManagedStatic<IntMapTy> IntConstants; 00251 00252 ConstantInt *ConstantInt::get(const Type *Ty, uint64_t V, bool isSigned) { 00253 const IntegerType *ITy = cast<IntegerType>(Ty); 00254 return get(APInt(ITy->getBitWidth(), V, isSigned)); 00255 } 00256 00257 // Get a ConstantInt from an APInt. Note that the value stored in the DenseMap 00258 // as the key, is a DenseMapAPIntKeyInfo::KeyTy which has provided the 00259 // operator== and operator!= to ensure that the DenseMap doesn't attempt to 00260 // compare APInt's of different widths, which would violate an APInt class 00261 // invariant which generates an assertion. 00262 ConstantInt *ConstantInt::get(const APInt& V) { 00263 // Get the corresponding integer type for the bit width of the value. 00264 const IntegerType *ITy = IntegerType::get(V.getBitWidth()); 00265 // get an existing value or the insertion position 00266 DenseMapAPIntKeyInfo::KeyTy Key(V, ITy); 00267 ConstantInt *&Slot = (*IntConstants)[Key]; 00268 // if it exists, return it. 00269 if (Slot) 00270 return Slot; 00271 // otherwise create a new one, insert it, and return it. 00272 return Slot = new ConstantInt(ITy, V); 00273 } 00274 00275 //===----------------------------------------------------------------------===// 00276 // ConstantFP 00277 //===----------------------------------------------------------------------===// 00278 00279 static const fltSemantics *TypeToFloatSemantics(const Type *Ty) { 00280 if (Ty == Type::FloatTy) 00281 return &APFloat::IEEEsingle; 00282 if (Ty == Type::DoubleTy) 00283 return &APFloat::IEEEdouble; 00284 if (Ty == Type::X86_FP80Ty) 00285 return &APFloat::x87DoubleExtended; 00286 else if (Ty == Type::FP128Ty) 00287 return &APFloat::IEEEquad; 00288 00289 assert(Ty == Type::PPC_FP128Ty && "Unknown FP format"); 00290 return &APFloat::PPCDoubleDouble; 00291 } 00292 00293 ConstantFP::ConstantFP(const Type *Ty, const APFloat& V) 00294 : Constant(Ty, ConstantFPVal, 0, 0), Val(V) { 00295 assert(&V.getSemantics() == TypeToFloatSemantics(Ty) && 00296 "FP type Mismatch"); 00297 } 00298 00299 bool ConstantFP::isNullValue() const { 00300 return Val.isZero() && !Val.isNegative(); 00301 } 00302 00303 ConstantFP *ConstantFP::getNegativeZero(const Type *Ty) { 00304 APFloat apf = cast <ConstantFP>(Constant::getNullValue(Ty))->getValueAPF(); 00305 apf.changeSign(); 00306 return ConstantFP::get(apf); 00307 } 00308 00309 bool ConstantFP::isExactlyValue(const APFloat& V) const { 00310 return Val.bitwiseIsEqual(V); 00311 } 00312 00313 namespace { 00314 struct DenseMapAPFloatKeyInfo { 00315 struct KeyTy { 00316 APFloat val; 00317 KeyTy(const APFloat& V) : val(V){} 00318 KeyTy(const KeyTy& that) : val(that.val) {} 00319 bool operator==(const KeyTy& that) const { 00320 return this->val.bitwiseIsEqual(that.val); 00321 } 00322 bool operator!=(const KeyTy& that) const { 00323 return !this->operator==(that); 00324 } 00325 }; 00326 static inline KeyTy getEmptyKey() { 00327 return KeyTy(APFloat(APFloat::Bogus,1)); 00328 } 00329 static inline KeyTy getTombstoneKey() { 00330 return KeyTy(APFloat(APFloat::Bogus,2)); 00331 } 00332 static unsigned getHashValue(const KeyTy &Key) { 00333 return Key.val.getHashValue(); 00334 } 00335 static bool isEqual(const KeyTy &LHS, const KeyTy &RHS) { 00336 return LHS == RHS; 00337 } 00338 static bool isPod() { return false; } 00339 }; 00340 } 00341 00342 //---- ConstantFP::get() implementation... 00343 // 00344 typedef DenseMap<DenseMapAPFloatKeyInfo::KeyTy, ConstantFP*, 00345 DenseMapAPFloatKeyInfo> FPMapTy; 00346 00347 static ManagedStatic<FPMapTy> FPConstants; 00348 00349 ConstantFP *ConstantFP::get(const APFloat &V) { 00350 DenseMapAPFloatKeyInfo::KeyTy Key(V); 00351 ConstantFP *&Slot = (*FPConstants)[Key]; 00352 if (Slot) return Slot; 00353 00354 const Type *Ty; 00355 if (&V.getSemantics() == &APFloat::IEEEsingle) 00356 Ty = Type::FloatTy; 00357 else if (&V.getSemantics() == &APFloat::IEEEdouble) 00358 Ty = Type::DoubleTy; 00359 else if (&V.getSemantics() == &APFloat::x87DoubleExtended) 00360 Ty = Type::X86_FP80Ty; 00361 else if (&V.getSemantics() == &APFloat::IEEEquad) 00362 Ty = Type::FP128Ty; 00363 else { 00364 assert(&V.getSemantics() == &APFloat::PPCDoubleDouble&&"Unknown FP format"); 00365 Ty = Type::PPC_FP128Ty; 00366 } 00367 00368 return Slot = new ConstantFP(Ty, V); 00369 } 00370 00371 /// get() - This returns a constant fp for the specified value in the 00372 /// specified type. This should only be used for simple constant values like 00373 /// 2.0/1.0 etc, that are known-valid both as double and as the target format. 00374 ConstantFP *ConstantFP::get(const Type *Ty, double V) { 00375 APFloat FV(V); 00376 bool ignored; 00377 FV.convert(*TypeToFloatSemantics(Ty), APFloat::rmNearestTiesToEven, &ignored); 00378 return get(FV); 00379 } 00380 00381 //===----------------------------------------------------------------------===// 00382 // ConstantXXX Classes 00383 //===----------------------------------------------------------------------===// 00384 00385 00386 ConstantArray::ConstantArray(const ArrayType *T, 00387 const std::vector<Constant*> &V) 00388 : Constant(T, ConstantArrayVal, 00389 OperandTraits<ConstantArray>::op_end(this) - V.size(), 00390 V.size()) { 00391 assert(V.size() == T->getNumElements() && 00392 "Invalid initializer vector for constant array"); 00393 Use *OL = OperandList; 00394 for (std::vector<Constant*>::const_iterator I = V.begin(), E = V.end(); 00395 I != E; ++I, ++OL) { 00396 Constant *C = *I; 00397 assert((C->getType() == T->getElementType() || 00398 (T->isAbstract() && 00399 C->getType()->getTypeID() == T->getElementType()->getTypeID())) && 00400 "Initializer for array element doesn't match array element type!"); 00401 *OL = C; 00402 } 00403 } 00404 00405 00406 ConstantStruct::ConstantStruct(const StructType *T, 00407 const std::vector<Constant*> &V) 00408 : Constant(T, ConstantStructVal, 00409 OperandTraits<ConstantStruct>::op_end(this) - V.size(), 00410 V.size()) { 00411 assert(V.size() == T->getNumElements() && 00412 "Invalid initializer vector for constant structure"); 00413 Use *OL = OperandList; 00414 for (std::vector<Constant*>::const_iterator I = V.begin(), E = V.end(); 00415 I != E; ++I, ++OL) { 00416 Constant *C = *I; 00417 assert((C->getType() == T->getElementType(I-V.begin()) || 00418 ((T->getElementType(I-V.begin())->isAbstract() || 00419 C->getType()->isAbstract()) && 00420 T->getElementType(I-V.begin())->getTypeID() == 00421 C->getType()->getTypeID())) && 00422 "Initializer for struct element doesn't match struct element type!"); 00423 *OL = C; 00424 } 00425 } 00426 00427 00428 ConstantVector::ConstantVector(const VectorType *T, 00429 const std::vector<Constant*> &V) 00430 : Constant(T, ConstantVectorVal, 00431 OperandTraits<ConstantVector>::op_end(this) - V.size(), 00432 V.size()) { 00433 Use *OL = OperandList; 00434 for (std::vector<Constant*>::const_iterator I = V.begin(), E = V.end(); 00435 I != E; ++I, ++OL) { 00436 Constant *C = *I; 00437 assert((C->getType() == T->getElementType() || 00438 (T->isAbstract() && 00439 C->getType()->getTypeID() == T->getElementType()->getTypeID())) && 00440 "Initializer for vector element doesn't match vector element type!"); 00441 *OL = C; 00442 } 00443 } 00444 00445 00446 namespace llvm { 00447 // We declare several classes private to this file, so use an anonymous 00448 // namespace 00449 namespace { 00450 00451 /// UnaryConstantExpr - This class is private to Constants.cpp, and is used 00452 /// behind the scenes to implement unary constant exprs. 00453 class VISIBILITY_HIDDEN UnaryConstantExpr : public ConstantExpr { 00454 void *operator new(size_t, unsigned); // DO NOT IMPLEMENT 00455 public: 00456 // allocate space for exactly one operand 00457 void *operator new(size_t s) { 00458 return User::operator new(s, 1); 00459 } 00460 UnaryConstantExpr(unsigned Opcode, Constant *C, const Type *Ty) 00461 : ConstantExpr(Ty, Opcode, &Op<0>(), 1) { 00462 Op<0>() = C; 00463 } 00464 /// Transparently provide more efficient getOperand methods. 00465 DECLARE_TRANSPARENT_OPERAND_ACCESSORS(Value); 00466 }; 00467 00468 /// BinaryConstantExpr - This class is private to Constants.cpp, and is used 00469 /// behind the scenes to implement binary constant exprs. 00470 class VISIBILITY_HIDDEN BinaryConstantExpr : public ConstantExpr { 00471 void *operator new(size_t, unsigned); // DO NOT IMPLEMENT 00472 public: 00473 // allocate space for exactly two operands 00474 void *operator new(size_t s) { 00475 return User::operator new(s, 2); 00476 } 00477 BinaryConstantExpr(unsigned Opcode, Constant *C1, Constant *C2) 00478 : ConstantExpr(C1->getType(), Opcode, &Op<0>(), 2) { 00479 Op<0>() = C1; 00480 Op<1>() = C2; 00481 } 00482 /// Transparently provide more efficient getOperand methods. 00483 DECLARE_TRANSPARENT_OPERAND_ACCESSORS(Value); 00484 }; 00485 00486 /// SelectConstantExpr - This class is private to Constants.cpp, and is used 00487 /// behind the scenes to implement select constant exprs. 00488 class VISIBILITY_HIDDEN SelectConstantExpr : public ConstantExpr { 00489 void *operator new(size_t, unsigned); // DO NOT IMPLEMENT 00490 public: 00491 // allocate space for exactly three operands 00492 void *operator new(size_t s) { 00493 return User::operator new(s, 3); 00494 } 00495 SelectConstantExpr(Constant *C1, Constant *C2, Constant *C3) 00496 : ConstantExpr(C2->getType(), Instruction::Select, &Op<0>(), 3) { 00497 Op<0>() = C1; 00498 Op<1>() = C2; 00499 Op<2>() = C3; 00500 } 00501 /// Transparently provide more efficient getOperand methods. 00502 DECLARE_TRANSPARENT_OPERAND_ACCESSORS(Value); 00503 }; 00504 00505 /// ExtractElementConstantExpr - This class is private to 00506 /// Constants.cpp, and is used behind the scenes to implement 00507 /// extractelement constant exprs. 00508 class VISIBILITY_HIDDEN ExtractElementConstantExpr : public ConstantExpr { 00509 void *operator new(size_t, unsigned); // DO NOT IMPLEMENT 00510 public: 00511 // allocate space for exactly two operands 00512 void *operator new(size_t s) { 00513 return User::operator new(s, 2); 00514 } 00515 ExtractElementConstantExpr(Constant *C1, Constant *C2) 00516 : ConstantExpr(cast<VectorType>(C1->getType())->getElementType(), 00517 Instruction::ExtractElement, &Op<0>(), 2) { 00518 Op<0>() = C1; 00519 Op<1>() = C2; 00520 } 00521 /// Transparently provide more efficient getOperand methods. 00522 DECLARE_TRANSPARENT_OPERAND_ACCESSORS(Value); 00523 }; 00524 00525 /// InsertElementConstantExpr - This class is private to 00526 /// Constants.cpp, and is used behind the scenes to implement 00527 /// insertelement constant exprs. 00528 class VISIBILITY_HIDDEN InsertElementConstantExpr : public ConstantExpr { 00529 void *operator new(size_t, unsigned); // DO NOT IMPLEMENT 00530 public: 00531 // allocate space for exactly three operands 00532 void *operator new(size_t s) { 00533 return User::operator new(s, 3); 00534 } 00535 InsertElementConstantExpr(Constant *C1, Constant *C2, Constant *C3) 00536 : ConstantExpr(C1->getType(), Instruction::InsertElement, 00537 &Op<0>(), 3) { 00538 Op<0>() = C1; 00539 Op<1>() = C2; 00540 Op<2>() = C3; 00541 } 00542 /// Transparently provide more efficient getOperand methods. 00543 DECLARE_TRANSPARENT_OPERAND_ACCESSORS(Value); 00544 }; 00545 00546 /// ShuffleVectorConstantExpr - This class is private to 00547 /// Constants.cpp, and is used behind the scenes to implement 00548 /// shufflevector constant exprs. 00549 class VISIBILITY_HIDDEN ShuffleVectorConstantExpr : public ConstantExpr { 00550 void *operator new(size_t, unsigned); // DO NOT IMPLEMENT 00551 public: 00552 // allocate space for exactly three operands 00553 void *operator new(size_t s) { 00554 return User::operator new(s, 3); 00555 } 00556 ShuffleVectorConstantExpr(Constant *C1, Constant *C2, Constant *C3) 00557 : ConstantExpr(C1->getType(), Instruction::ShuffleVector, 00558 &Op<0>(), 3) { 00559 Op<0>() = C1; 00560 Op<1>() = C2; 00561 Op<2>() = C3; 00562 } 00563 /// Transparently provide more efficient getOperand methods. 00564 DECLARE_TRANSPARENT_OPERAND_ACCESSORS(Value); 00565 }; 00566 00567 /// ExtractValueConstantExpr - This class is private to 00568 /// Constants.cpp, and is used behind the scenes to implement 00569 /// extractvalue constant exprs. 00570 class VISIBILITY_HIDDEN ExtractValueConstantExpr : public ConstantExpr { 00571 void *operator new(size_t, unsigned); // DO NOT IMPLEMENT 00572 public: 00573 // allocate space for exactly one operand 00574 void *operator new(size_t s) { 00575 return User::operator new(s, 1); 00576 } 00577 ExtractValueConstantExpr(Constant *Agg, 00578 const SmallVector<unsigned, 4> &IdxList, 00579 const Type *DestTy) 00580 : ConstantExpr(DestTy, Instruction::ExtractValue, &Op<0>(), 1), 00581 Indices(IdxList) { 00582 Op<0>() = Agg; 00583 } 00584 00585 /// Indices - These identify which value to extract. 00586 const SmallVector<unsigned, 4> Indices; 00587 00588 /// Transparently provide more efficient getOperand methods. 00589 DECLARE_TRANSPARENT_OPERAND_ACCESSORS(Value); 00590 }; 00591 00592 /// InsertValueConstantExpr - This class is private to 00593 /// Constants.cpp, and is used behind the scenes to implement 00594 /// insertvalue constant exprs. 00595 class VISIBILITY_HIDDEN InsertValueConstantExpr : public ConstantExpr { 00596 void *operator new(size_t, unsigned); // DO NOT IMPLEMENT 00597 public: 00598 // allocate space for exactly one operand 00599 void *operator new(size_t s) { 00600 return User::operator new(s, 2); 00601 } 00602 InsertValueConstantExpr(Constant *Agg, Constant *Val, 00603 const SmallVector<unsigned, 4> &IdxList, 00604 const Type *DestTy) 00605 : ConstantExpr(DestTy, Instruction::InsertValue, &Op<0>(), 2), 00606 Indices(IdxList) { 00607 Op<0>() = Agg; 00608 Op<1>() = Val; 00609 } 00610 00611 /// Indices - These identify the position for the insertion. 00612 const SmallVector<unsigned, 4> Indices; 00613 00614 /// Transparently provide more efficient getOperand methods. 00615 DECLARE_TRANSPARENT_OPERAND_ACCESSORS(Value); 00616 }; 00617 00618 00619 /// GetElementPtrConstantExpr - This class is private to Constants.cpp, and is 00620 /// used behind the scenes to implement getelementpr constant exprs. 00621 class VISIBILITY_HIDDEN GetElementPtrConstantExpr : public ConstantExpr { 00622 GetElementPtrConstantExpr(Constant *C, const std::vector<Constant*> &IdxList, 00623 const Type *DestTy); 00624 public: 00625 static GetElementPtrConstantExpr *Create(Constant *C, 00626 const std::vector<Constant*>&IdxList, 00627 const Type *DestTy) { 00628 return new(IdxList.size() + 1) 00629 GetElementPtrConstantExpr(C, IdxList, DestTy); 00630 } 00631 /// Transparently provide more efficient getOperand methods. 00632 DECLARE_TRANSPARENT_OPERAND_ACCESSORS(Value); 00633 }; 00634 00635 // CompareConstantExpr - This class is private to Constants.cpp, and is used 00636 // behind the scenes to implement ICmp and FCmp constant expressions. This is 00637 // needed in order to store the predicate value for these instructions. 00638 struct VISIBILITY_HIDDEN CompareConstantExpr : public ConstantExpr { 00639 void *operator new(size_t, unsigned); // DO NOT IMPLEMENT 00640 // allocate space for exactly two operands 00641 void *operator new(size_t s) { 00642 return User::operator new(s, 2); 00643 } 00644 unsigned short predicate; 00645 CompareConstantExpr(const Type *ty, Instruction::OtherOps opc, 00646 unsigned short pred, Constant* LHS, Constant* RHS) 00647 : ConstantExpr(ty, opc, &Op<0>(), 2), predicate(pred) { 00648 Op<0>() = LHS; 00649 Op<1>() = RHS; 00650 } 00651 /// Transparently provide more efficient getOperand methods. 00652 DECLARE_TRANSPARENT_OPERAND_ACCESSORS(Value); 00653 }; 00654 00655 } // end anonymous namespace 00656 00657 template <> 00658 struct OperandTraits<UnaryConstantExpr> : FixedNumOperandTraits<1> { 00659 }; 00660 DEFINE_TRANSPARENT_OPERAND_ACCESSORS(UnaryConstantExpr, Value) 00661 00662 template <> 00663 struct OperandTraits<BinaryConstantExpr> : FixedNumOperandTraits<2> { 00664 }; 00665 DEFINE_TRANSPARENT_OPERAND_ACCESSORS(BinaryConstantExpr, Value) 00666 00667 template <> 00668 struct OperandTraits<SelectConstantExpr> : FixedNumOperandTraits<3> { 00669 }; 00670 DEFINE_TRANSPARENT_OPERAND_ACCESSORS(SelectConstantExpr, Value) 00671 00672 template <> 00673 struct OperandTraits<ExtractElementConstantExpr> : FixedNumOperandTraits<2> { 00674 }; 00675 DEFINE_TRANSPARENT_OPERAND_ACCESSORS(ExtractElementConstantExpr, Value) 00676 00677 template <> 00678 struct OperandTraits<InsertElementConstantExpr> : FixedNumOperandTraits<3> { 00679 }; 00680 DEFINE_TRANSPARENT_OPERAND_ACCESSORS(InsertElementConstantExpr, Value) 00681 00682 template <> 00683 struct OperandTraits<ShuffleVectorConstantExpr> : FixedNumOperandTraits<3> { 00684 }; 00685 DEFINE_TRANSPARENT_OPERAND_ACCESSORS(ShuffleVectorConstantExpr, Value) 00686 00687 template <> 00688 struct OperandTraits<ExtractValueConstantExpr> : FixedNumOperandTraits<1> { 00689 }; 00690 DEFINE_TRANSPARENT_OPERAND_ACCESSORS(ExtractValueConstantExpr, Value) 00691 00692 template <> 00693 struct OperandTraits<InsertValueConstantExpr> : FixedNumOperandTraits<2> { 00694 }; 00695 DEFINE_TRANSPARENT_OPERAND_ACCESSORS(InsertValueConstantExpr, Value) 00696 00697 template <> 00698 struct OperandTraits<GetElementPtrConstantExpr> : VariadicOperandTraits<1> { 00699 }; 00700 00701 GetElementPtrConstantExpr::GetElementPtrConstantExpr 00702 (Constant *C, 00703 const std::vector<Constant*> &IdxList, 00704 const Type *DestTy) 00705 : ConstantExpr(DestTy, Instruction::GetElementPtr, 00706 OperandTraits<GetElementPtrConstantExpr>::op_end(this) 00707 - (IdxList.size()+1), 00708 IdxList.size()+1) { 00709 OperandList[0] = C; 00710 for (unsigned i = 0, E = IdxList.size(); i != E; ++i) 00711 OperandList[i+1] = IdxList[i]; 00712 } 00713 00714 DEFINE_TRANSPARENT_OPERAND_ACCESSORS(GetElementPtrConstantExpr, Value) 00715 00716 00717 template <> 00718 struct OperandTraits<CompareConstantExpr> : FixedNumOperandTraits<2> { 00719 }; 00720 DEFINE_TRANSPARENT_OPERAND_ACCESSORS(CompareConstantExpr, Value) 00721 00722 00723 } // End llvm namespace 00724 00725 00726 // Utility function for determining if a ConstantExpr is a CastOp or not. This 00727 // can't be inline because we don't want to #include Instruction.h into 00728 // Constant.h 00729 bool ConstantExpr::isCast() const { 00730 return Instruction::isCast(getOpcode()); 00731 } 00732 00733 bool ConstantExpr::isCompare() const { 00734 return getOpcode() == Instruction::ICmp || getOpcode() == Instruction::FCmp || 00735 getOpcode() == Instruction::VICmp || getOpcode() == Instruction::VFCmp; 00736 } 00737 00738 bool ConstantExpr::hasIndices() const { 00739 return getOpcode() == Instruction::ExtractValue || 00740 getOpcode() == Instruction::InsertValue; 00741 } 00742 00743 const SmallVector<unsigned, 4> &ConstantExpr::getIndices() const { 00744 if (const ExtractValueConstantExpr *EVCE = 00745 dyn_cast<ExtractValueConstantExpr>(this)) 00746 return EVCE->Indices; 00747 00748 return cast<InsertValueConstantExpr>(this)->Indices; 00749 } 00750 00751 /// ConstantExpr::get* - Return some common constants without having to 00752 /// specify the full Instruction::OPCODE identifier. 00753 /// 00754 Constant *ConstantExpr::getNeg(Constant *C) { 00755 return get(Instruction::Sub, 00756 ConstantExpr::getZeroValueForNegationExpr(C->getType()), 00757 C); 00758 } 00759 Constant *ConstantExpr::getNot(Constant *C) { 00760 assert((isa<IntegerType>(C->getType()) || 00761 cast<VectorType>(C->getType())->getElementType()->isInteger()) && 00762 "Cannot NOT a nonintegral value!"); 00763 return get(Instruction::Xor, C, 00764 Constant::getAllOnesValue(C->getType())); 00765 } 00766 Constant *ConstantExpr::getAdd(Constant *C1, Constant *C2) { 00767 return get(Instruction::Add, C1, C2); 00768 } 00769 Constant *ConstantExpr::getSub(Constant *C1, Constant *C2) { 00770 return get(Instruction::Sub, C1, C2); 00771 } 00772 Constant *ConstantExpr::getMul(Constant *C1, Constant *C2) { 00773 return get(Instruction::Mul, C1, C2); 00774 } 00775 Constant *ConstantExpr::getUDiv(Constant *C1, Constant *C2) { 00776 return get(Instruction::UDiv, C1, C2); 00777 } 00778 Constant *ConstantExpr::getSDiv(Constant *C1, Constant *C2) { 00779 return get(Instruction::SDiv, C1, C2); 00780 } 00781 Constant *ConstantExpr::getFDiv(Constant *C1, Constant *C2) { 00782 return get(Instruction::FDiv, C1, C2); 00783 } 00784 Constant *ConstantExpr::getURem(Constant *C1, Constant *C2) { 00785 return get(Instruction::URem, C1, C2); 00786 } 00787 Constant *ConstantExpr::getSRem(Constant *C1, Constant *C2) { 00788 return get(Instruction::SRem, C1, C2); 00789 } 00790 Constant *ConstantExpr::getFRem(Constant *C1, Constant *C2) { 00791 return get(Instruction::FRem, C1, C2); 00792 } 00793 Constant *ConstantExpr::getAnd(Constant *C1, Constant *C2) { 00794 return get(Instruction::And, C1, C2); 00795 } 00796 Constant *ConstantExpr::getOr(Constant *C1, Constant *C2) { 00797 return get(Instruction::Or, C1, C2); 00798 } 00799 Constant *ConstantExpr::getXor(Constant *C1, Constant *C2) { 00800 return get(Instruction::Xor, C1, C2); 00801 } 00802 unsigned ConstantExpr::getPredicate() const { 00803 assert(getOpcode() == Instruction::FCmp || 00804 getOpcode() == Instruction::ICmp || 00805 getOpcode() == Instruction::VFCmp || 00806 getOpcode() == Instruction::VICmp); 00807 return ((const CompareConstantExpr*)this)->predicate; 00808 } 00809 Constant *ConstantExpr::getShl(Constant *C1, Constant *C2) { 00810 return get(Instruction::Shl, C1, C2); 00811 } 00812 Constant *ConstantExpr::getLShr(Constant *C1, Constant *C2) { 00813 return get(Instruction::LShr, C1, C2); 00814 } 00815 Constant *ConstantExpr::getAShr(Constant *C1, Constant *C2) { 00816 return get(Instruction::AShr, C1, C2); 00817 } 00818 00819 /// getWithOperandReplaced - Return a constant expression identical to this 00820 /// one, but with the specified operand set to the specified value. 00821 Constant * 00822 ConstantExpr::getWithOperandReplaced(unsigned OpNo, Constant *Op) const { 00823 assert(OpNo < getNumOperands() && "Operand num is out of range!"); 00824 assert(Op->getType() == getOperand(OpNo)->getType() && 00825 "Replacing operand with value of different type!"); 00826 if (getOperand(OpNo) == Op) 00827 return const_cast<ConstantExpr*>(this); 00828 00829 Constant *Op0, *Op1, *Op2; 00830 switch (getOpcode()) { 00831 case Instruction::Trunc: 00832 case Instruction::ZExt: 00833 case Instruction::SExt: 00834 case Instruction::FPTrunc: 00835 case Instruction::FPExt: 00836 case Instruction::UIToFP: 00837 case Instruction::SIToFP: 00838 case Instruction::FPToUI: 00839 case Instruction::FPToSI: 00840 case Instruction::PtrToInt: 00841 case Instruction::IntToPtr: 00842 case Instruction::BitCast: 00843 return ConstantExpr::getCast(getOpcode(), Op, getType()); 00844 case Instruction::Select: 00845 Op0 = (OpNo == 0) ? Op : getOperand(0); 00846 Op1 = (OpNo == 1) ? Op : getOperand(1); 00847 Op2 = (OpNo == 2) ? Op : getOperand(2); 00848 return ConstantExpr::getSelect(Op0, Op1, Op2); 00849 case Instruction::InsertElement: 00850 Op0 = (OpNo == 0) ? Op : getOperand(0); 00851 Op1 = (OpNo == 1) ? Op : getOperand(1); 00852 Op2 = (OpNo == 2) ? Op : getOperand(2); 00853 return ConstantExpr::getInsertElement(Op0, Op1, Op2); 00854 case Instruction::ExtractElement: 00855 Op0 = (OpNo == 0) ? Op : getOperand(0); 00856 Op1 = (OpNo == 1) ? Op : getOperand(1); 00857 return ConstantExpr::getExtractElement(Op0, Op1); 00858 case Instruction::ShuffleVector: 00859 Op0 = (OpNo == 0) ? Op : getOperand(0); 00860 Op1 = (OpNo == 1) ? Op : getOperand(1); 00861 Op2 = (OpNo == 2) ? Op : getOperand(2); 00862 return ConstantExpr::getShuffleVector(Op0, Op1, Op2); 00863 case Instruction::GetElementPtr: { 00864 SmallVector<Constant*, 8> Ops; 00865 Ops.resize(getNumOperands()-1); 00866 for (unsigned i = 1, e = getNumOperands(); i != e; ++i) 00867 Ops[i-1] = getOperand(i); 00868 if (OpNo == 0) 00869 return ConstantExpr::getGetElementPtr(Op, &Ops[0], Ops.size()); 00870 Ops[OpNo-1] = Op; 00871 return ConstantExpr::getGetElementPtr(getOperand(0), &Ops[0], Ops.size()); 00872 } 00873 default: 00874 assert(getNumOperands() == 2 && "Must be binary operator?"); 00875 Op0 = (OpNo == 0) ? Op : getOperand(0); 00876 Op1 = (OpNo == 1) ? Op : getOperand(1); 00877 return ConstantExpr::get(getOpcode(), Op0, Op1); 00878 } 00879 } 00880 00881 /// getWithOperands - This returns the current constant expression with the 00882 /// operands replaced with the specified values. The specified operands must 00883 /// match count and type with the existing ones. 00884 Constant *ConstantExpr:: 00885 getWithOperands(Constant* const *Ops, unsigned NumOps) const { 00886 assert(NumOps == getNumOperands() && "Operand count mismatch!"); 00887 bool AnyChange = false; 00888 for (unsigned i = 0; i != NumOps; ++i) { 00889 assert(Ops[i]->getType() == getOperand(i)->getType() && 00890 "Operand type mismatch!"); 00891 AnyChange |= Ops[i] != getOperand(i); 00892 } 00893 if (!AnyChange) // No operands changed, return self. 00894 return const_cast<ConstantExpr*>(this); 00895 00896 switch (getOpcode()) { 00897 case Instruction::Trunc: 00898 case Instruction::ZExt: 00899 case Instruction::SExt: 00900 case Instruction::FPTrunc: 00901 case Instruction::FPExt: 00902 case Instruction::UIToFP: 00903 case Instruction::SIToFP: 00904 case Instruction::FPToUI: 00905 case Instruction::FPToSI: 00906 case Instruction::PtrToInt: 00907 case Instruction::IntToPtr: 00908 case Instruction::BitCast: 00909 return ConstantExpr::getCast(getOpcode(), Ops[0], getType()); 00910 case Instruction::Select: 00911 return ConstantExpr::getSelect(Ops[0], Ops[1], Ops[2]); 00912 case Instruction::InsertElement: 00913 return ConstantExpr::getInsertElement(Ops[0], Ops[1], Ops[2]); 00914 case Instruction::ExtractElement: 00915 return ConstantExpr::getExtractElement(Ops[0], Ops[1]); 00916 case Instruction::ShuffleVector: 00917 return ConstantExpr::getShuffleVector(Ops[0], Ops[1], Ops[2]); 00918 case Instruction::GetElementPtr: 00919 return ConstantExpr::getGetElementPtr(Ops[0], &Ops[1], NumOps-1); 00920 case Instruction::ICmp: 00921 case Instruction::FCmp: 00922 case Instruction::VICmp: 00923 case Instruction::VFCmp: 00924 return ConstantExpr::getCompare(getPredicate(), Ops[0], Ops[1]); 00925 default: 00926 assert(getNumOperands() == 2 && "Must be binary operator?"); 00927 return ConstantExpr::get(getOpcode(), Ops[0], Ops[1]); 00928 } 00929 } 00930 00931 00932 //===----------------------------------------------------------------------===// 00933 // isValueValidForType implementations 00934 00935 bool ConstantInt::isValueValidForType(const Type *Ty, uint64_t Val) { 00936 unsigned NumBits = cast<IntegerType>(Ty)->getBitWidth(); // assert okay 00937 if (Ty == Type::Int1Ty) 00938 return Val == 0 || Val == 1; 00939 if (NumBits >= 64) 00940 return true; // always true, has to fit in largest type 00941 uint64_t Max = (1ll << NumBits) - 1; 00942 return Val <= Max; 00943 } 00944 00945 bool ConstantInt::isValueValidForType(const Type *Ty, int64_t Val) { 00946 unsigned NumBits = cast<IntegerType>(Ty)->getBitWidth(); // assert okay 00947 if (Ty == Type::Int1Ty) 00948 return Val == 0 || Val == 1 || Val == -1; 00949 if (NumBits >= 64) 00950 return true; // always true, has to fit in largest type 00951 int64_t Min = -(1ll << (NumBits-1)); 00952 int64_t Max = (1ll << (NumBits-1)) - 1; 00953 return (Val >= Min && Val <= Max); 00954 } 00955 00956 bool ConstantFP::isValueValidForType(const Type *Ty, const APFloat& Val) { 00957 // convert modifies in place, so make a copy. 00958 APFloat Val2 = APFloat(Val); 00959 bool losesInfo; 00960 switch (Ty->getTypeID()) { 00961 default: 00962 return false; // These can't be represented as floating point! 00963 00964 // FIXME rounding mode needs to be more flexible 00965 case Type::FloatTyID: { 00966 if (&Val2.getSemantics() == &APFloat::IEEEsingle) 00967 return true; 00968 Val2.convert(APFloat::IEEEsingle, APFloat::rmNearestTiesToEven, &losesInfo); 00969 return !losesInfo; 00970 } 00971 case Type::DoubleTyID: { 00972 if (&Val2.getSemantics() == &APFloat::IEEEsingle || 00973 &Val2.getSemantics() == &APFloat::IEEEdouble) 00974 return true; 00975 Val2.convert(APFloat::IEEEdouble, APFloat::rmNearestTiesToEven, &losesInfo); 00976 return !losesInfo; 00977 } 00978 case Type::X86_FP80TyID: 00979 return &Val2.getSemantics() == &APFloat::IEEEsingle || 00980 &Val2.getSemantics() == &APFloat::IEEEdouble || 00981 &Val2.getSemantics() == &APFloat::x87DoubleExtended; 00982 case Type::FP128TyID: 00983 return &Val2.getSemantics() == &APFloat::IEEEsingle || 00984 &Val2.getSemantics() == &APFloat::IEEEdouble || 00985 &Val2.getSemantics() == &APFloat::IEEEquad; 00986 case Type::PPC_FP128TyID: 00987 return &Val2.getSemantics() == &APFloat::IEEEsingle || 00988 &Val2.getSemantics() == &APFloat::IEEEdouble || 00989 &Val2.getSemantics() == &APFloat::PPCDoubleDouble; 00990 } 00991 } 00992 00993 //===----------------------------------------------------------------------===// 00994 // Factory Function Implementation 00995 00996 00997 // The number of operands for each ConstantCreator::create method is 00998 // determined by the ConstantTraits template. 00999 // ConstantCreator - A class that is used to create constants by 01000 // ValueMap*. This class should be partially specialized if there is 01001 // something strange that needs to be done to interface to the ctor for the 01002 // constant. 01003 // 01004 namespace llvm { 01005 template<class ValType> 01006 struct ConstantTraits; 01007 01008 template<typename T, typename Alloc> 01009 struct VISIBILITY_HIDDEN ConstantTraits< std::vector<T, Alloc> > { 01010 static unsigned uses(const std::vector<T, Alloc>& v) { 01011 return v.size(); 01012 } 01013 }; 01014 01015 template<class ConstantClass, class TypeClass, class ValType> 01016 struct VISIBILITY_HIDDEN ConstantCreator { 01017 static ConstantClass *create(const TypeClass *Ty, const ValType &V) { 01018 return new(ConstantTraits<ValType>::uses(V)) ConstantClass(Ty, V); 01019 } 01020 }; 01021 01022 template<class ConstantClass, class TypeClass> 01023 struct VISIBILITY_HIDDEN ConvertConstantType { 01024 static void convert(ConstantClass *OldC, const TypeClass *NewTy) { 01025 assert(0 && "This type cannot be converted!\n"); 01026 abort(); 01027 } 01028 }; 01029 01030 template<class ValType, class TypeClass, class ConstantClass, 01031 bool HasLargeKey = false /*true for arrays and structs*/ > 01032 class VISIBILITY_HIDDEN ValueMap : public AbstractTypeUser { 01033 public: 01034 typedef std::pair<const Type*, ValType> MapKey; 01035 typedef std::map<MapKey, Constant *> MapTy; 01036 typedef std::map<Constant*, typename MapTy::iterator> InverseMapTy; 01037 typedef std::map<const Type*, typename MapTy::iterator> AbstractTypeMapTy; 01038 private: 01039 /// Map - This is the main map from the element descriptor to the Constants. 01040 /// This is the primary way we avoid creating two of the same shape 01041 /// constant. 01042 MapTy Map; 01043 01044 /// InverseMap - If "HasLargeKey" is true, this contains an inverse mapping 01045 /// from the constants to their element in Map. This is important for 01046 /// removal of constants from the array, which would otherwise have to scan 01047 /// through the map with very large keys. 01048 InverseMapTy InverseMap; 01049 01050 /// AbstractTypeMap - Map for abstract type constants. 01051 /// 01052 AbstractTypeMapTy AbstractTypeMap; 01053 01054 public: 01055 typename MapTy::iterator map_end() { return Map.end(); } 01056 01057 /// InsertOrGetItem - Return an iterator for the specified element. 01058 /// If the element exists in the map, the returned iterator points to the 01059 /// entry and Exists=true. If not, the iterator points to the newly 01060 /// inserted entry and returns Exists=false. Newly inserted entries have 01061 /// I->second == 0, and should be filled in. 01062 typename MapTy::iterator InsertOrGetItem(std::pair<MapKey, Constant *> 01063 &InsertVal, 01064 bool &Exists) { 01065 std::pair<typename MapTy::iterator, bool> IP = Map.insert(InsertVal); 01066 Exists = !IP.second; 01067 return IP.first; 01068 } 01069 01070 private: 01071 typename MapTy::iterator FindExistingElement(ConstantClass *CP) { 01072 if (HasLargeKey) { 01073 typename InverseMapTy::iterator IMI = InverseMap.find(CP); 01074 assert(IMI != InverseMap.end() && IMI->second != Map.end() && 01075 IMI->second->second == CP && 01076 "InverseMap corrupt!"); 01077 return IMI->second; 01078 } 01079 01080 typename MapTy::iterator I = 01081 Map.find(MapKey(static_cast<const TypeClass*>(CP->getRawType()), 01082 getValType(CP))); 01083 if (I == Map.end() || I->second != CP) { 01084 // FIXME: This should not use a linear scan. If this gets to be a 01085 // performance problem, someone should look at this. 01086 for (I = Map.begin(); I != Map.end() && I->second != CP; ++I) 01087 /* empty */; 01088 } 01089 return I; 01090 } 01091 public: 01092 01093 /// getOrCreate - Return the specified constant from the map, creating it if 01094 /// necessary. 01095 ConstantClass *getOrCreate(const TypeClass *Ty, const ValType &V) { 01096 MapKey Lookup(Ty, V); 01097 typename MapTy::iterator I = Map.find(Lookup); 01098 // Is it in the map? 01099 if (I != Map.end()) 01100 return static_cast<ConstantClass *>(I->second); 01101 01102 // If no preexisting value, create one now... 01103 ConstantClass *Result = 01104 ConstantCreator<ConstantClass,TypeClass,ValType>::create(Ty, V); 01105 01106 assert(Result->getType() == Ty && "Type specified is not correct!"); 01107 I = Map.insert(I, std::make_pair(MapKey(Ty, V), Result)); 01108 01109 if (HasLargeKey) // Remember the reverse mapping if needed. 01110 InverseMap.insert(std::make_pair(Result, I)); 01111 01112 // If the type of the constant is abstract, make sure that an entry exists 01113 // for it in the AbstractTypeMap. 01114 if (Ty->isAbstract()) { 01115 typename AbstractTypeMapTy::iterator TI = AbstractTypeMap.find(Ty); 01116 01117 if (TI == AbstractTypeMap.end()) { 01118 // Add ourselves to the ATU list of the type. 01119 cast<DerivedType>(Ty)->addAbstractTypeUser(this); 01120 01121 AbstractTypeMap.insert(TI, std::make_pair(Ty, I)); 01122 } 01123 } 01124 return Result; 01125 } 01126 01127 void remove(ConstantClass *CP) { 01128 typename MapTy::iterator I = FindExistingElement(CP); 01129 assert(I != Map.end() && "Constant not found in constant table!"); 01130 assert(I->second == CP && "Didn't find correct element?"); 01131 01132 if (HasLargeKey) // Remember the reverse mapping if needed. 01133 InverseMap.erase(CP); 01134 01135 // Now that we found the entry, make sure this isn't the entry that 01136 // the AbstractTypeMap points to. 01137 const TypeClass *Ty = static_cast<const TypeClass *>(I->first.first); 01138 if (Ty->isAbstract()) { 01139 assert(AbstractTypeMap.count(Ty) && 01140 "Abstract type not in AbstractTypeMap?"); 01141 typename MapTy::iterator &ATMEntryIt = AbstractTypeMap[Ty]; 01142 if (ATMEntryIt == I) { 01143 // Yes, we are removing the representative entry for this type. 01144 // See if there are any other entries of the same type. 01145 typename MapTy::iterator TmpIt = ATMEntryIt; 01146 01147 // First check the entry before this one... 01148 if (TmpIt != Map.begin()) { 01149 --TmpIt; 01150 if (TmpIt->first.first != Ty) // Not the same type, move back... 01151 ++TmpIt; 01152 } 01153 01154 // If we didn't find the same type, try to move forward... 01155 if (TmpIt == ATMEntryIt) { 01156 ++TmpIt; 01157 if (TmpIt == Map.end() || TmpIt->first.first != Ty) 01158 --TmpIt; // No entry afterwards with the same type 01159 } 01160 01161 // If there is another entry in the map of the same abstract type, 01162 // update the AbstractTypeMap entry now. 01163 if (TmpIt != ATMEntryIt) { 01164 ATMEntryIt = TmpIt; 01165 } else { 01166 // Otherwise, we are removing the last instance of this type 01167 // from the table. Remove from the ATM, and from user list. 01168 cast<DerivedType>(Ty)->removeAbstractTypeUser(this); 01169 AbstractTypeMap.erase(Ty); 01170 } 01171 } 01172 } 01173 01174 Map.erase(I); 01175 } 01176 01177 01178 /// MoveConstantToNewSlot - If we are about to change C to be the element 01179 /// specified by I, update our internal data structures to reflect this 01180 /// fact. 01181 void MoveConstantToNewSlot(ConstantClass *C, typename MapTy::iterator I) { 01182 // First, remove the old location of the specified constant in the map. 01183 typename MapTy::iterator OldI = FindExistingElement(C); 01184 assert(OldI != Map.end() && "Constant not found in constant table!"); 01185 assert(OldI->second == C && "Didn't find correct element?"); 01186 01187 // If this constant is the representative element for its abstract type, 01188 // update the AbstractTypeMap so that the representative element is I. 01189 if (C->getType()->isAbstract()) { 01190 typename AbstractTypeMapTy::iterator ATI = 01191 AbstractTypeMap.find(C->getType()); 01192 assert(ATI != AbstractTypeMap.end() && 01193 "Abstract type not in AbstractTypeMap?"); 01194 if (ATI->second == OldI) 01195 ATI->second = I; 01196 } 01197 01198 // Remove the old entry from the map. 01199 Map.erase(OldI); 01200 01201 // Update the inverse map so that we know that this constant is now 01202 // located at descriptor I. 01203 if (HasLargeKey) { 01204 assert(I->second == C && "Bad inversemap entry!"); 01205 InverseMap[C] = I; 01206 } 01207 } 01208 01209 void refineAbstractType(const DerivedType *OldTy, const Type *NewTy) { 01210 typename AbstractTypeMapTy::iterator I = 01211 AbstractTypeMap.find(cast<Type>(OldTy)); 01212 01213 assert(I != AbstractTypeMap.end() && 01214 "Abstract type not in AbstractTypeMap?"); 01215 01216 // Convert a constant at a time until the last one is gone. The last one 01217 // leaving will remove() itself, causing the AbstractTypeMapEntry to be 01218 // eliminated eventually. 01219 do { 01220 ConvertConstantType<ConstantClass, 01221 TypeClass>::convert( 01222 static_cast<ConstantClass *>(I->second->second), 01223 cast<TypeClass>(NewTy)); 01224 01225 I = AbstractTypeMap.find(cast<Type>(OldTy)); 01226 } while (I != AbstractTypeMap.end()); 01227 } 01228 01229 // If the type became concrete without being refined to any other existing 01230 // type, we just remove ourselves from the ATU list. 01231 void typeBecameConcrete(const DerivedType *AbsTy) { 01232 AbsTy->removeAbstractTypeUser(this); 01233 } 01234 01235 void dump() const { 01236 DOUT << "Constant.cpp: ValueMap\n"; 01237 } 01238 }; 01239 } 01240 01241 01242 01243 //---- ConstantAggregateZero::get() implementation... 01244 // 01245 namespace llvm { 01246 // ConstantAggregateZero does not take extra "value" argument... 01247 template<class ValType> 01248 struct ConstantCreator<ConstantAggregateZero, Type, ValType> { 01249 static ConstantAggregateZero *create(const Type *Ty, const ValType &V){ 01250 return new ConstantAggregateZero(Ty); 01251 } 01252 }; 01253 01254 template<> 01255 struct ConvertConstantType<ConstantAggregateZero, Type> { 01256 static void convert(ConstantAggregateZero *OldC, const Type *NewTy) { 01257 // Make everyone now use a constant of the new type... 01258 Constant *New = ConstantAggregateZero::get(NewTy); 01259 assert(New != OldC && "Didn't replace constant??"); 01260 OldC->uncheckedReplaceAllUsesWith(New); 01261 OldC->destroyConstant(); // This constant is now dead, destroy it. 01262 } 01263 }; 01264 } 01265 01266 static ManagedStatic<ValueMap<char, Type, 01267 ConstantAggregateZero> > AggZeroConstants; 01268 01269 static char getValType(ConstantAggregateZero *CPZ) { return 0; } 01270 01271 ConstantAggregateZero *ConstantAggregateZero::get(const Type *Ty) { 01272 assert((isa<StructType>(Ty) || isa<ArrayType>(Ty) || isa<VectorType>(Ty)) && 01273 "Cannot create an aggregate zero of non-aggregate type!"); 01274 return AggZeroConstants->getOrCreate(Ty, 0); 01275 } 01276 01277 // destroyConstant - Remove the constant from the constant table... 01278 // 01279 void ConstantAggregateZero::destroyConstant() { 01280 AggZeroConstants->remove(this); 01281 destroyConstantImpl(); 01282 } 01283 01284 //---- ConstantArray::get() implementation... 01285 // 01286 namespace llvm { 01287 template<> 01288 struct ConvertConstantType<ConstantArray, ArrayType> { 01289 static void convert(ConstantArray *OldC, const ArrayType *NewTy) { 01290 // Make everyone now use a constant of the new type... 01291 std::vector<Constant*> C; 01292 for (unsigned i = 0, e = OldC->getNumOperands(); i != e; ++i) 01293 C.push_back(cast<Constant>(OldC->getOperand(i))); 01294 Constant *New = ConstantArray::get(NewTy, C); 01295 assert(New != OldC && "Didn't replace constant??"); 01296 OldC->uncheckedReplaceAllUsesWith(New); 01297 OldC->destroyConstant(); // This constant is now dead, destroy it. 01298 } 01299 }; 01300 } 01301 01302 static std::vector<Constant*> getValType(ConstantArray *CA) { 01303 std::vector<Constant*> Elements; 01304 Elements.reserve(CA->getNumOperands()); 01305 for (unsigned i = 0, e = CA->getNumOperands(); i != e; ++i) 01306 Elements.push_back(cast<Constant>(CA->getOperand(i))); 01307 return Elements; 01308 } 01309 01310 typedef ValueMap<std::vector<Constant*>, ArrayType, 01311 ConstantArray, true /*largekey*/> ArrayConstantsTy; 01312 static ManagedStatic<ArrayConstantsTy> ArrayConstants; 01313 01314 Constant *ConstantArray::get(const ArrayType *Ty, 01315 const std::vector<Constant*> &V) { 01316 // If this is an all-zero array, return a ConstantAggregateZero object 01317 if (!V.empty()) { 01318 Constant *C = V[0]; 01319 if (!C->isNullValue()) 01320 return ArrayConstants->getOrCreate(Ty, V); 01321 for (unsigned i = 1, e = V.size(); i != e; ++i) 01322 if (V[i] != C) 01323 return ArrayConstants->getOrCreate(Ty, V); 01324 } 01325 return ConstantAggregateZero::get(Ty); 01326 } 01327 01328 // destroyConstant - Remove the constant from the constant table... 01329 // 01330 void ConstantArray::destroyConstant() { 01331 ArrayConstants->remove(this); 01332 destroyConstantImpl(); 01333 } 01334 01335 /// ConstantArray::get(const string&) - Return an array that is initialized to 01336 /// contain the specified string. If length is zero then a null terminator is 01337 /// added to the specified string so that it may be used in a natural way. 01338 /// Otherwise, the length parameter specifies how much of the string to use 01339 /// and it won't be null terminated. 01340 /// 01341 Constant *ConstantArray::get(const std::string &Str, bool AddNull) { 01342 std::vector<Constant*> ElementVals; 01343 for (unsigned i = 0; i < Str.length(); ++i) 01344 ElementVals.push_back(ConstantInt::get(Type::Int8Ty, Str[i])); 01345 01346 // Add a null terminator to the string... 01347 if (AddNull) { 01348 ElementVals.push_back(ConstantInt::get(Type::Int8Ty, 0)); 01349 } 01350 01351 ArrayType *ATy = ArrayType::get(Type::Int8Ty, ElementVals.size()); 01352 return ConstantArray::get(ATy, ElementVals); 01353 } 01354 01355 /// isString - This method returns true if the array is an array of i8, and 01356 /// if the elements of the array are all ConstantInt's. 01357 bool ConstantArray::isString() const { 01358 // Check the element type for i8... 01359 if (getType()->getElementType() != Type::Int8Ty) 01360 return false; 01361 // Check the elements to make sure they are all integers, not constant 01362 // expressions. 01363 for (unsigned i = 0, e = getNumOperands(); i != e; ++i) 01364 if (!isa<ConstantInt>(getOperand(i))) 01365 return false; 01366 return true; 01367 } 01368 01369 /// isCString - This method returns true if the array is a string (see 01370 /// isString) and it ends in a null byte \0 and does not contains any other 01371 /// null bytes except its terminator. 01372 bool ConstantArray::isCString() const { 01373 // Check the element type for i8... 01374 if (getType()->getElementType() != Type::Int8Ty) 01375 return false; 01376 Constant *Zero = Constant::getNullValue(getOperand(0)->getType()); 01377 // Last element must be a null. 01378 if (getOperand(getNumOperands()-1) != Zero) 01379 return false; 01380 // Other elements must be non-null integers. 01381 for (unsigned i = 0, e = getNumOperands()-1; i != e; ++i) { 01382 if (!isa<ConstantInt>(getOperand(i))) 01383 return false; 01384 if (getOperand(i) == Zero) 01385 return false; 01386 } 01387 return true; 01388 } 01389 01390 01391 // getAsString - If the sub-element type of this array is i8 01392 // then this method converts the array to an std::string and returns it. 01393 // Otherwise, it asserts out. 01394 // 01395 std::string ConstantArray::getAsString() const { 01396 assert(isString() && "Not a string!"); 01397 std::string Result; 01398 Result.reserve(getNumOperands()); 01399 for (unsigned i = 0, e = getNumOperands(); i != e; ++i) 01400 Result.push_back((char)cast<ConstantInt>(getOperand(i))->getZExtValue()); 01401 return Result; 01402 } 01403 01404 01405 //---- ConstantStruct::get() implementation... 01406 // 01407 01408 namespace llvm { 01409 template<> 01410 struct ConvertConstantType<ConstantStruct, StructType> { 01411 static void convert(ConstantStruct *OldC, const StructType *NewTy) { 01412 // Make everyone now use a constant of the new type... 01413 std::vector<Constant*> C; 01414 for (unsigned i = 0, e = OldC->getNumOperands(); i != e; ++i) 01415 C.push_back(cast<Constant>(OldC->getOperand(i))); 01416 Constant *New = ConstantStruct::get(NewTy, C); 01417 assert(New != OldC && "Didn't replace constant??"); 01418 01419 OldC->uncheckedReplaceAllUsesWith(New); 01420 OldC->destroyConstant(); // This constant is now dead, destroy it. 01421 } 01422 }; 01423 } 01424 01425 typedef ValueMap<std::vector<Constant*>, StructType, 01426 ConstantStruct, true /*largekey*/> StructConstantsTy; 01427 static ManagedStatic<StructConstantsTy> StructConstants; 01428 01429 static std::vector<Constant*> getValType(ConstantStruct *CS) { 01430 std::vector<Constant*> Elements; 01431 Elements.reserve(CS->getNumOperands()); 01432 for (unsigned i = 0, e = CS->getNumOperands(); i != e; ++i) 01433 Elements.push_back(cast<Constant>(CS->getOperand(i))); 01434 return Elements; 01435 } 01436 01437 Constant *ConstantStruct::get(const StructType *Ty, 01438 const std::vector<Constant*> &V) { 01439 // Create a ConstantAggregateZero value if all elements are zeros... 01440 for (unsigned i = 0, e = V.size(); i != e; ++i) 01441 if (!V[i]->isNullValue()) 01442 return StructConstants->getOrCreate(Ty, V); 01443 01444 return ConstantAggregateZero::get(Ty); 01445 } 01446 01447 Constant *ConstantStruct::get(const std::vector<Constant*> &V, bool packed) { 01448 std::vector<const Type*> StructEls; 01449 StructEls.reserve(V.size()); 01450 for (unsigned i = 0, e = V.size(); i != e; ++i) 01451 StructEls.push_back(V[i]->getType()); 01452 return get(StructType::get(StructEls, packed), V); 01453 } 01454 01455 // destroyConstant - Remove the constant from the constant table... 01456 // 01457 void ConstantStruct::destroyConstant() { 01458 StructConstants->remove(this); 01459 destroyConstantImpl(); 01460 } 01461 01462 //---- ConstantVector::get() implementation... 01463 // 01464 namespace llvm { 01465 template<> 01466 struct ConvertConstantType<ConstantVector, VectorType> { 01467 static void convert(ConstantVector *OldC, const VectorType *NewTy) { 01468 // Make everyone now use a constant of the new type... 01469 std::vector<Constant*> C; 01470 for (unsigned i = 0, e = OldC->getNumOperands(); i != e; ++i) 01471 C.push_back(cast<Constant>(OldC->getOperand(i))); 01472 Constant *New = ConstantVector::get(NewTy, C); 01473 assert(New != OldC && "Didn't replace constant??"); 01474 OldC->uncheckedReplaceAllUsesWith(New); 01475 OldC->destroyConstant(); // This constant is now dead, destroy it. 01476 } 01477 }; 01478 } 01479 01480 static std::vector<Constant*> getValType(ConstantVector *CP) { 01481 std::vector<Constant*> Elements; 01482 Elements.reserve(CP->getNumOperands()); 01483 for (unsigned i = 0, e = CP->getNumOperands(); i != e; ++i) 01484 Elements.push_back(CP->getOperand(i)); 01485 return Elements; 01486 } 01487 01488 static ManagedStatic<ValueMap<std::vector<Constant*>, VectorType, 01489 ConstantVector> > VectorConstants; 01490 01491 Constant *ConstantVector::get(const VectorType *Ty, 01492 const std::vector<Constant*> &V) { 01493 assert(!V.empty() && "Vectors can't be empty"); 01494 // If this is an all-undef or alll-zero vector, return a 01495 // ConstantAggregateZero or UndefValue. 01496 Constant *C = V[0]; 01497 bool isZero = C->isNullValue(); 01498 bool isUndef = isa<UndefValue>(C); 01499 01500 if (isZero || isUndef) { 01501 for (unsigned i = 1, e = V.size(); i != e; ++i) 01502 if (V[i] != C) { 01503 isZero = isUndef = false; 01504 break; 01505 } 01506 } 01507 01508 if (isZero) 01509 return ConstantAggregateZero::get(Ty); 01510 if (isUndef) 01511 return UndefValue::get(Ty); 01512 return VectorConstants->getOrCreate(Ty, V); 01513 } 01514 01515 Constant *ConstantVector::get(const std::vector<Constant*> &V) { 01516 assert(!V.empty() && "Cannot infer type if V is empty"); 01517 return get(VectorType::get(V.front()->getType(),V.size()), V); 01518 } 01519 01520 // destroyConstant - Remove the constant from the constant table... 01521 // 01522 void ConstantVector::destroyConstant() { 01523 VectorConstants->remove(this); 01524 destroyConstantImpl(); 01525 } 01526 01527 /// This function will return true iff every element in this vector constant 01528 /// is set to all ones. 01529 /// @returns true iff this constant's emements are all set to all ones. 01530 /// @brief Determine if the value is all ones. 01531 bool ConstantVector::isAllOnesValue() const { 01532 // Check out first element. 01533 const Constant *Elt = getOperand(0); 01534 const ConstantInt *CI = dyn_cast<ConstantInt>(Elt); 01535 if (!CI || !CI->isAllOnesValue()) return false; 01536 // Then make sure all remaining elements point to the same value. 01537 for (unsigned I = 1, E = getNumOperands(); I < E; ++I) { 01538 if (getOperand(I) != Elt) return false; 01539 } 01540 return true; 01541 } 01542 01543 /// getSplatValue - If this is a splat constant, where all of the 01544 /// elements have the same value, return that value. Otherwise return null. 01545 Constant *ConstantVector::getSplatValue() { 01546 // Check out first element. 01547 Constant *Elt = getOperand(0); 01548 // Then make sure all remaining elements point to the same value. 01549 for (unsigned I = 1, E = getNumOperands(); I < E; ++I) 01550 if (getOperand(I) != Elt) return 0; 01551 return Elt; 01552 } 01553 01554 //---- ConstantPointerNull::get() implementation... 01555 // 01556 01557 namespace llvm { 01558 // ConstantPointerNull does not take extra "value" argument... 01559 template<class ValType> 01560 struct ConstantCreator<ConstantPointerNull, PointerType, ValType> { 01561 static ConstantPointerNull *create(const PointerType *Ty, const ValType &V){ 01562 return new ConstantPointerNull(Ty); 01563 } 01564 }; 01565 01566 template<> 01567 struct ConvertConstantType<ConstantPointerNull, PointerType> { 01568 static void convert(ConstantPointerNull *OldC, const PointerType *NewTy) { 01569 // Make everyone now use a constant of the new type... 01570 Constant *New = ConstantPointerNull::get(NewTy); 01571 assert(New != OldC && "Didn't replace constant??"); 01572 OldC->uncheckedReplaceAllUsesWith(New); 01573 OldC->destroyConstant(); // This constant is now dead, destroy it. 01574 } 01575 }; 01576 } 01577 01578 static ManagedStatic<ValueMap<char, PointerType, 01579 ConstantPointerNull> > NullPtrConstants; 01580 01581 static char getValType(ConstantPointerNull *) { 01582 return 0; 01583 } 01584 01585 01586 ConstantPointerNull *ConstantPointerNull::get(const PointerType *Ty) { 01587 return NullPtrConstants->getOrCreate(Ty, 0); 01588 } 01589 01590 // destroyConstant - Remove the constant from the constant table... 01591 // 01592 void ConstantPointerNull::destroyConstant() { 01593 NullPtrConstants->remove(this); 01594 destroyConstantImpl(); 01595 } 01596 01597 01598 //---- UndefValue::get() implementation... 01599 // 01600 01601 namespace llvm { 01602 // UndefValue does not take extra "value" argument... 01603 template<class ValType> 01604 struct ConstantCreator<UndefValue, Type, ValType> { 01605 static UndefValue *create(const Type *Ty, const ValType &V) { 01606 return new UndefValue(Ty); 01607 } 01608 }; 01609 01610 template<> 01611 struct ConvertConstantType<UndefValue, Type> { 01612 static void convert(UndefValue *OldC, const Type *NewTy) { 01613 // Make everyone now use a constant of the new type. 01614 Constant *New = UndefValue::get(NewTy); 01615 assert(New != OldC && "Didn't replace constant??"); 01616 OldC->uncheckedReplaceAllUsesWith(New); 01617 OldC->destroyConstant(); // This constant is now dead, destroy it. 01618 } 01619 }; 01620 } 01621 01622 static ManagedStatic<ValueMap<char, Type, UndefValue> > UndefValueConstants; 01623 01624 static char getValType(UndefValue *) { 01625 return 0; 01626 } 01627 01628 01629 UndefValue *UndefValue::get(const Type *Ty) { 01630 return UndefValueConstants->getOrCreate(Ty, 0); 01631 } 01632 01633 // destroyConstant - Remove the constant from the constant table. 01634 // 01635 void UndefValue::destroyConstant() { 01636 UndefValueConstants->remove(this); 01637 destroyConstantImpl(); 01638 } 01639 01640 01641 //---- ConstantExpr::get() implementations... 01642 // 01643 01644 namespace { 01645 01646 struct ExprMapKeyType { 01647 typedef SmallVector<unsigned, 4> IndexList; 01648 01649 ExprMapKeyType(unsigned opc, 01650 const std::vector<Constant*> &ops, 01651 unsigned short pred = 0, 01652 const IndexList &inds = IndexList()) 01653 : opcode(opc), predicate(pred), operands(ops), indices(inds) {} 01654 uint16_t opcode; 01655 uint16_t predicate; 01656 std::vector<Constant*> operands; 01657 IndexList indices; 01658 bool operator==(const ExprMapKeyType& that) const { 01659 return this->opcode == that.opcode && 01660 this->predicate == that.predicate && 01661 this->operands == that.operands && 01662 this->indices == that.indices; 01663 } 01664 bool operator<(const ExprMapKeyType & that) const { 01665 return this->opcode < that.opcode || 01666 (this->opcode == that.opcode && this->predicate < that.predicate) || 01667 (this->opcode == that.opcode && this->predicate == that.predicate && 01668 this->operands < that.operands) || 01669 (this->opcode == that.opcode && this->predicate == that.predicate && 01670 this->operands == that.operands && this->indices < that.indices); 01671 } 01672 01673 bool operator!=(const ExprMapKeyType& that) const { 01674 return !(*this == that); 01675 } 01676 }; 01677 01678 } 01679 01680 namespace llvm { 01681 template<> 01682 struct ConstantCreator<ConstantExpr, Type, ExprMapKeyType> { 01683 static ConstantExpr *create(const Type *Ty, const ExprMapKeyType &V, 01684 unsigned short pred = 0) { 01685 if (Instruction::isCast(V.opcode)) 01686 return new UnaryConstantExpr(V.opcode, V.operands[0], Ty); 01687 if ((V.opcode >= Instruction::BinaryOpsBegin && 01688 V.opcode < Instruction::BinaryOpsEnd)) 01689 return new BinaryConstantExpr(V.opcode, V.operands[0], V.operands[1]); 01690 if (V.opcode == Instruction::Select) 01691 return new SelectConstantExpr(V.operands[0], V.operands[1], 01692 V.operands[2]); 01693 if (V.opcode == Instruction::ExtractElement) 01694 return new ExtractElementConstantExpr(V.operands[0], V.operands[1]); 01695 if (V.opcode == Instruction::InsertElement) 01696 return new InsertElementConstantExpr(V.operands[0], V.operands[1], 01697 V.operands[2]); 01698 if (V.opcode == Instruction::ShuffleVector) 01699 return new ShuffleVectorConstantExpr(V.operands[0], V.operands[1], 01700 V.operands[2]); 01701 if (V.opcode == Instruction::InsertValue) 01702 return new InsertValueConstantExpr(V.operands[0], V.operands[1], 01703 V.indices, Ty); 01704 if (V.opcode == Instruction::ExtractValue) 01705 return new ExtractValueConstantExpr(V.operands[0], V.indices, Ty); 01706 if (V.opcode == Instruction::GetElementPtr) { 01707 std::vector<Constant*> IdxList(V.operands.begin()+1, V.operands.end()); 01708 return GetElementPtrConstantExpr::Create(V.operands[0], IdxList, Ty); 01709 } 01710 01711 // The compare instructions are weird. We have to encode the predicate 01712 // value and it is combined with the instruction opcode by multiplying 01713 // the opcode by one hundred. We must decode this to get the predicate. 01714 if (V.opcode == Instruction::ICmp) 01715 return new CompareConstantExpr(Ty, Instruction::ICmp, V.predicate, 01716 V.operands[0], V.operands[1]); 01717 if (V.opcode == Instruction::FCmp) 01718 return new CompareConstantExpr(Ty, Instruction::FCmp, V.predicate, 01719 V.operands[0], V.operands[1]); 01720 if (V.opcode == Instruction::VICmp) 01721 return new CompareConstantExpr(Ty, Instruction::VICmp, V.predicate, 01722 V.operands[0], V.operands[1]); 01723 if (V.opcode == Instruction::VFCmp) 01724 return new CompareConstantExpr(Ty, Instruction::VFCmp, V.predicate, 01725 V.operands[0], V.operands[1]); 01726 assert(0 && "Invalid ConstantExpr!"); 01727 return 0; 01728 } 01729 }; 01730 01731 template<> 01732 struct ConvertConstantType<ConstantExpr, Type> { 01733 static void convert(ConstantExpr *OldC, const Type *NewTy) { 01734 Constant *New; 01735 switch (OldC->getOpcode()) { 01736 case Instruction::Trunc: 01737 case Instruction::ZExt: 01738 case Instruction::SExt: 01739 case Instruction::FPTrunc: 01740 case Instruction::FPExt: 01741 case Instruction::UIToFP: 01742 case Instruction::SIToFP: 01743 case Instruction::FPToUI: 01744 case Instruction::FPToSI: 01745 case Instruction::PtrToInt: 01746 case Instruction::IntToPtr: 01747 case Instruction::BitCast: 01748 New = ConstantExpr::getCast(OldC->getOpcode(), OldC->getOperand(0), 01749 NewTy); 01750 break; 01751 case Instruction::Select: 01752 New = ConstantExpr::getSelectTy(NewTy, OldC->getOperand(0), 01753 OldC->getOperand(1), 01754 OldC->getOperand(2)); 01755 break; 01756 default: 01757 assert(OldC->getOpcode() >= Instruction::BinaryOpsBegin && 01758 OldC->getOpcode() < Instruction::BinaryOpsEnd); 01759 New = ConstantExpr::getTy(NewTy, OldC->getOpcode(), OldC->getOperand(0), 01760 OldC->getOperand(1)); 01761 break; 01762 case Instruction::GetElementPtr: 01763 // Make everyone now use a constant of the new type... 01764 std::vector<Value*> Idx(OldC->op_begin()+1, OldC->op_end()); 01765 New = ConstantExpr::getGetElementPtrTy(NewTy, OldC->getOperand(0), 01766 &Idx[0], Idx.size()); 01767 break; 01768 } 01769 01770 assert(New != OldC && "Didn't replace constant??"); 01771 OldC->uncheckedReplaceAllUsesWith(New); 01772 OldC->destroyConstant(); // This constant is now dead, destroy it. 01773 } 01774 }; 01775 } // end namespace llvm 01776 01777 01778 static ExprMapKeyType getValType(ConstantExpr *CE) { 01779 std::vector<Constant*> Operands; 01780 Operands.reserve(CE->getNumOperands()); 01781 for (unsigned i = 0, e = CE->getNumOperands(); i != e; ++i) 01782 Operands.push_back(cast<Constant>(CE->getOperand(i))); 01783 return ExprMapKeyType(CE->getOpcode(), Operands, 01784 CE->isCompare() ? CE->getPredicate() : 0, 01785 CE->hasIndices() ? 01786 CE->getIndices() : SmallVector<unsigned, 4>()); 01787 } 01788 01789 static ManagedStatic<ValueMap<ExprMapKeyType, Type, 01790 ConstantExpr> > ExprConstants; 01791 01792 /// This is a utility function to handle folding of casts and lookup of the 01793 /// cast in the ExprConstants map. It is used by the various get* methods below. 01794 static inline Constant *getFoldedCast( 01795 Instruction::CastOps opc, Constant *C, const Type *Ty) { 01796 assert(Ty->isFirstClassType() && "Cannot cast to an aggregate type!"); 01797 // Fold a few common cases 01798 if (Constant *FC = ConstantFoldCastInstruction(opc, C, Ty)) 01799 return FC; 01800 01801 // Look up the constant in the table first to ensure uniqueness 01802 std::vector<Constant*> argVec(1, C); 01803 ExprMapKeyType Key(opc, argVec); 01804 return ExprConstants->getOrCreate(Ty, Key); 01805 } 01806 01807 Constant *ConstantExpr::getCast(unsigned oc, Constant *C, const Type *Ty) { 01808 Instruction::CastOps opc = Instruction::CastOps(oc); 01809 assert(Instruction::isCast(opc) && "opcode out of range"); 01810 assert(C && Ty && "Null arguments to getCast"); 01811 assert(Ty->isFirstClassType() && "Cannot cast to an aggregate type!"); 01812 01813 switch (opc) { 01814 default: 01815 assert(0 && "Invalid cast opcode"); 01816 break; 01817 case Instruction::Trunc: return getTrunc(C, Ty); 01818 case Instruction::ZExt: return getZExt(C, Ty); 01819 case Instruction::SExt: return getSExt(C, Ty); 01820 case Instruction::FPTrunc: return getFPTrunc(C, Ty); 01821 case Instruction::FPExt: return getFPExtend(C, Ty); 01822 case Instruction::UIToFP: return getUIToFP(C, Ty); 01823 case Instruction::SIToFP: return getSIToFP(C, Ty); 01824 case Instruction::FPToUI: return getFPToUI(C, Ty); 01825 case Instruction::FPToSI: return getFPToSI(C, Ty); 01826 case Instruction::PtrToInt: return getPtrToInt(C, Ty); 01827 case Instruction::IntToPtr: return getIntToPtr(C, Ty); 01828 case Instruction::BitCast: return getBitCast(C, Ty); 01829 } 01830 return 0; 01831 } 01832 01833 Constant *ConstantExpr::getZExtOrBitCast(Constant *C, const Type *Ty) { 01834 if (C->getType()->getPrimitiveSizeInBits() == Ty->getPrimitiveSizeInBits()) 01835 return getCast(Instruction::BitCast, C, Ty); 01836 return getCast(Instruction::ZExt, C, Ty); 01837 } 01838 01839 Constant *ConstantExpr::getSExtOrBitCast(Constant *C, const Type *Ty) { 01840 if (C->getType()->getPrimitiveSizeInBits() == Ty->getPrimitiveSizeInBits()) 01841 return getCast(Instruction::BitCast, C, Ty); 01842 return getCast(Instruction::SExt, C, Ty); 01843 } 01844 01845 Constant *ConstantExpr::getTruncOrBitCast(Constant *C, const Type *Ty) { 01846 if (C->getType()->getPrimitiveSizeInBits() == Ty->getPrimitiveSizeInBits()) 01847 return getCast(Instruction::BitCast, C, Ty); 01848 return getCast(Instruction::Trunc, C, Ty); 01849 } 01850 01851 Constant *ConstantExpr::getPointerCast(Constant *S, const Type *Ty) { 01852 assert(isa<PointerType>(S->getType()) && "Invalid cast"); 01853 assert((Ty->isInteger() || isa<PointerType>(Ty)) && "Invalid cast"); 01854 01855 if (Ty->isInteger()) 01856 return getCast(Instruction::PtrToInt, S, Ty); 01857 return getCast(Instruction::BitCast, S, Ty); 01858 } 01859 01860 Constant *ConstantExpr::getIntegerCast(Constant *C, const Type *Ty, 01861 bool isSigned) { 01862 assert(C->getType()->isInteger() && Ty->isInteger() && "Invalid cast"); 01863 unsigned SrcBits = C->getType()->getPrimitiveSizeInBits(); 01864 unsigned DstBits = Ty->getPrimitiveSizeInBits(); 01865 Instruction::CastOps opcode = 01866 (SrcBits == DstBits ? Instruction::BitCast : 01867 (SrcBits > DstBits ? Instruction::Trunc : 01868 (isSigned ? Instruction::SExt : Instruction::ZExt))); 01869 return getCast(opcode, C, Ty); 01870 } 01871 01872 Constant *ConstantExpr::getFPCast(Constant *C, const Type *Ty) { 01873 assert(C->getType()->isFloatingPoint() && Ty->isFloatingPoint() && 01874 "Invalid cast"); 01875 unsigned SrcBits = C->getType()->getPrimitiveSizeInBits(); 01876 unsigned DstBits = Ty->getPrimitiveSizeInBits(); 01877 if (SrcBits == DstBits) 01878 return C; // Avoid a useless cast 01879 Instruction::CastOps opcode = 01880 (SrcBits > DstBits ? Instruction::FPTrunc : Instruction::FPExt); 01881 return getCast(opcode, C, Ty); 01882 } 01883 01884 Constant *ConstantExpr::getTrunc(Constant *C, const Type *Ty) { 01885 assert(C->getType()->isInteger() && "Trunc operand must be integer"); 01886 assert(Ty->isInteger() && "Trunc produces only integral"); 01887 assert(C->getType()->getPrimitiveSizeInBits() > Ty->getPrimitiveSizeInBits()&& 01888 "SrcTy must be larger than DestTy for Trunc!"); 01889 01890 return getFoldedCast(Instruction::Trunc, C, Ty); 01891 } 01892 01893 Constant *ConstantExpr::getSExt(Constant *C, const Type *Ty) { 01894 assert(C->getType()->isInteger() && "SEXt operand must be integral"); 01895 assert(Ty->isInteger() && "SExt produces only integer"); 01896 assert(C->getType()->getPrimitiveSizeInBits() < Ty->getPrimitiveSizeInBits()&& 01897 "SrcTy must be smaller than DestTy for SExt!"); 01898 01899 return getFoldedCast(Instruction::SExt, C, Ty); 01900 } 01901 01902 Constant *ConstantExpr::getZExt(Constant *C, const Type *Ty) { 01903 assert(C->getType()->isInteger() && "ZEXt operand must be integral"); 01904 assert(Ty->isInteger() && "ZExt produces only integer"); 01905 assert(C->getType()->getPrimitiveSizeInBits() < Ty->getPrimitiveSizeInBits()&& 01906 "SrcTy must be smaller than DestTy for ZExt!"); 01907 01908 return getFoldedCast(Instruction::ZExt, C, Ty); 01909 } 01910 01911 Constant *ConstantExpr::getFPTrunc(Constant *C, const Type *Ty) { 01912 assert(C->getType()->isFloatingPoint() && Ty->isFloatingPoint() && 01913 C->getType()->getPrimitiveSizeInBits() > Ty->getPrimitiveSizeInBits()&& 01914 "This is an illegal floating point truncation!"); 01915 return getFoldedCast(Instruction::FPTrunc, C, Ty); 01916 } 01917 01918 Constant *ConstantExpr::getFPExtend(Constant *C, const Type *Ty) { 01919 assert(C->getType()->isFloatingPoint() && Ty->isFloatingPoint() && 01920 C->getType()->getPrimitiveSizeInBits() < Ty->getPrimitiveSizeInBits()&& 01921 "This is an illegal floating point extension!"); 01922 return getFoldedCast(Instruction::FPExt, C, Ty); 01923 } 01924 01925 Constant *ConstantExpr::getUIToFP(Constant *C, const Type *Ty) { 01926 #ifndef NDEBUG 01927 bool fromVec = C->getType()->getTypeID() == Type::VectorTyID; 01928 bool toVec = Ty->getTypeID() == Type::VectorTyID; 01929 #endif 01930 assert((fromVec == toVec) && "Cannot convert from scalar to/from vector"); 01931 assert(C->getType()->isIntOrIntVector() && Ty->isFPOrFPVector() && 01932 "This is an illegal uint to floating point cast!"); 01933 return getFoldedCast(Instruction::UIToFP, C, Ty); 01934 } 01935 01936 Constant *ConstantExpr::getSIToFP(Constant *C, const Type *Ty) { 01937 #ifndef NDEBUG 01938 bool fromVec = C->getType()->getTypeID() == Type::VectorTyID; 01939 bool toVec = Ty->getTypeID() == Type::VectorTyID; 01940 #endif 01941 assert((fromVec == toVec) && "Cannot convert from scalar to/from vector"); 01942 assert(C->getType()->isIntOrIntVector() && Ty->isFPOrFPVector() && 01943 "This is an illegal sint to floating point cast!"); 01944 return getFoldedCast(Instruction::SIToFP, C, Ty); 01945 } 01946 01947 Constant *ConstantExpr::getFPToUI(Constant *C, const Type *Ty) { 01948 #ifndef NDEBUG 01949 bool fromVec = C->getType()->getTypeID() == Type::VectorTyID; 01950 bool toVec = Ty->getTypeID() == Type::VectorTyID; 01951 #endif 01952 assert((fromVec == toVec) && "Cannot convert from scalar to/from vector"); 01953 assert(C->getType()->isFPOrFPVector() && Ty->isIntOrIntVector() && 01954 "This is an illegal floating point to uint cast!"); 01955 return getFoldedCast(Instruction::FPToUI, C, Ty); 01956 } 01957 01958 Constant *ConstantExpr::getFPToSI(Constant *C, const Type *Ty) { 01959 #ifndef NDEBUG 01960 bool fromVec = C->getType()->getTypeID() == Type::VectorTyID; 01961 bool toVec = Ty->getTypeID() == Type::VectorTyID; 01962 #endif 01963 assert((fromVec == toVec) && "Cannot convert from scalar to/from vector"); 01964 assert(C->getType()->isFPOrFPVector() && Ty->isIntOrIntVector() && 01965 "This is an illegal floating point to sint cast!"); 01966 return getFoldedCast(Instruction::FPToSI, C, Ty); 01967 } 01968 01969 Constant *ConstantExpr::getPtrToInt(Constant *C, const Type *DstTy) { 01970 assert(isa<PointerType>(C->getType()) && "PtrToInt source must be pointer"); 01971 assert(DstTy->isInteger() && "PtrToInt destination must be integral"); 01972 return getFoldedCast(Instruction::PtrToInt, C, DstTy); 01973 } 01974 01975 Constant *ConstantExpr::getIntToPtr(Constant *C, const Type *DstTy) { 01976 assert(C->getType()->isInteger() && "IntToPtr source must be integral"); 01977 assert(isa<PointerType>(DstTy) && "IntToPtr destination must be a pointer"); 01978 return getFoldedCast(Instruction::IntToPtr, C, DstTy); 01979 } 01980 01981 Constant *ConstantExpr::getBitCast(Constant *C, const Type *DstTy) { 01982 // BitCast implies a no-op cast of type only. No bits change. However, you 01983 // can't cast pointers to anything but pointers. 01984 #ifndef NDEBUG 01985 const Type *SrcTy = C->getType(); 01986 assert((isa<PointerType>(SrcTy) == isa<PointerType>(DstTy)) && 01987 "BitCast cannot cast pointer to non-pointer and vice versa"); 01988 01989 // Now we know we're not dealing with mismatched pointer casts (ptr->nonptr 01990 // or nonptr->ptr). For all the other types, the cast is okay if source and 01991 // destination bit widths are identical. 01992 unsigned SrcBitSize = SrcTy->getPrimitiveSizeInBits(); 01993 unsigned DstBitSize = DstTy->getPrimitiveSizeInBits(); 01994 #endif 01995 assert(SrcBitSize == DstBitSize && "BitCast requies types of same width"); 01996 return getFoldedCast(Instruction::BitCast, C, DstTy); 01997 } 01998 01999 Constant *ConstantExpr::getSizeOf(const Type *Ty) { 02000 // sizeof is implemented as: (i64) gep (Ty*)null, 1 02001 Constant *GEPIdx = ConstantInt::get(Type::Int32Ty, 1); 02002 Constant *GEP = 02003 getGetElementPtr(getNullValue(PointerType::getUnqual(Ty)), &GEPIdx, 1); 02004 return getCast(Instruction::PtrToInt, GEP, Type::Int64Ty); 02005 } 02006 02007 Constant *ConstantExpr::getTy(const Type *ReqTy, unsigned Opcode, 02008 Constant *C1, Constant *C2) { 02009 // Check the operands for consistency first 02010 assert(Opcode >= Instruction::BinaryOpsBegin && 02011 Opcode < Instruction::BinaryOpsEnd && 02012 "Invalid opcode in binary constant expression"); 02013 assert(C1->getType() == C2->getType() && 02014 "Operand types in binary constant expression should match"); 02015 02016 if (ReqTy == C1->getType() || ReqTy == Type::Int1Ty) 02017 if (Constant *FC = ConstantFoldBinaryInstruction(Opcode, C1, C2)) 02018 return FC; // Fold a few common cases... 02019 02020 std::vector<Constant*> argVec(1, C1); argVec.push_back(C2); 02021 ExprMapKeyType Key(Opcode, argVec); 02022 return ExprConstants->getOrCreate(ReqTy, Key); 02023 } 02024 02025 Constant *ConstantExpr::getCompareTy(unsigned short predicate, 02026 Constant *C1, Constant *C2) { 02027 bool isVectorType = C1->getType()->getTypeID() == Type::VectorTyID; 02028 switch (predicate) { 02029 default: assert(0 && "Invalid CmpInst predicate"); 02030 case CmpInst::FCMP_FALSE: case CmpInst::FCMP_OEQ: case CmpInst::FCMP_OGT: 02031 case CmpInst::FCMP_OGE: case CmpInst::FCMP_OLT: case CmpInst::FCMP_OLE: 02032 case CmpInst::FCMP_ONE: case CmpInst::FCMP_ORD: case CmpInst::FCMP_UNO: 02033 case CmpInst::FCMP_UEQ: case CmpInst::FCMP_UGT: case CmpInst::FCMP_UGE: 02034 case CmpInst::FCMP_ULT: case CmpInst::FCMP_ULE: case CmpInst::FCMP_UNE: 02035 case CmpInst::FCMP_TRUE: 02036 return isVectorType ? getVFCmp(predicate, C1, C2) 02037 : getFCmp(predicate, C1, C2); 02038 case CmpInst::ICMP_EQ: case CmpInst::ICMP_NE: case CmpInst::ICMP_UGT: 02039 case CmpInst::ICMP_UGE: case CmpInst::ICMP_ULT: case CmpInst::ICMP_ULE: 02040 case CmpInst::ICMP_SGT: case CmpInst::ICMP_SGE: case CmpInst::ICMP_SLT: 02041 case CmpInst::ICMP_SLE: 02042 return isVectorType ? getVICmp(predicate, C1, C2) 02043 : getICmp(predicate, C1, C2); 02044 } 02045 } 02046 02047 Constant *ConstantExpr::get(unsigned Opcode, Constant *C1, Constant *C2) { 02048 #ifndef NDEBUG 02049 switch (Opcode) { 02050 case Instruction::Add: 02051 case Instruction::Sub: 02052 case Instruction::Mul: 02053 assert(C1->getType() == C2->getType() && "Op types should be identical!"); 02054 assert((C1->getType()->isInteger() || C1->getType()->isFloatingPoint() || 02055 isa<VectorType>(C1->getType())) && 02056 "Tried to create an arithmetic operation on a non-arithmetic type!"); 02057 break; 02058 case Instruction::UDiv: 02059 case Instruction::SDiv: 02060 assert(C1->getType() == C2->getType() && "Op types should be identical!"); 02061 assert((C1->getType()->isInteger() || (isa<VectorType>(C1->getType()) && 02062 cast<VectorType>(C1->getType())->getElementType()->isInteger())) && 02063 "Tried to create an arithmetic operation on a non-arithmetic type!"); 02064 break; 02065 case Instruction::FDiv: 02066 assert(C1->getType() == C2->getType() && "Op types should be identical!"); 02067 assert((C1->getType()->isFloatingPoint() || (isa<VectorType>(C1->getType()) 02068 && cast<VectorType>(C1->getType())->getElementType()->isFloatingPoint())) 02069 && "Tried to create an arithmetic operation on a non-arithmetic type!"); 02070 break; 02071 case Instruction::URem: 02072 case Instruction::SRem: 02073 assert(C1->getType() == C2->getType() && "Op types should be identical!"); 02074 assert((C1->getType()->isInteger() || (isa<VectorType>(C1->getType()) && 02075 cast<VectorType>(C1->getType())->getElementType()->isInteger())) && 02076 "Tried to create an arithmetic operation on a non-arithmetic type!"); 02077 break; 02078 case Instruction::FRem: 02079 assert(C1->getType() == C2->getType() && "Op types should be identical!"); 02080 assert((C1->getType()->isFloatingPoint() || (isa<VectorType>(C1->getType()) 02081 && cast<VectorType>(C1->getType())->getElementType()->isFloatingPoint())) 02082 && "Tried to create an arithmetic operation on a non-arithmetic type!"); 02083 break; 02084 case Instruction::And: 02085 case Instruction::Or: 02086 case Instruction::Xor: 02087 assert(C1->getType() == C2->getType() && "Op types should be identical!"); 02088 assert((C1->getType()->isInteger() || isa<VectorType>(C1->getType())) && 02089 "Tried to create a logical operation on a non-integral type!"); 02090 break; 02091 case Instruction::Shl: 02092 case Instruction::LShr: 02093 case Instruction::AShr: 02094 assert(C1->getType() == C2->getType() && "Op types should be identical!"); 02095 assert(C1->getType()->isInteger() && 02096 "Tried to create a shift operation on a non-integer type!"); 02097 break; 02098 default: 02099 break; 02100 } 02101 #endif 02102 02103 return getTy(C1->getType(), Opcode, C1, C2); 02104 } 02105 02106 Constant *ConstantExpr::getCompare(unsigned short pred, 02107 Constant *C1, Constant *C2) { 02108 assert(C1->getType() == C2->getType() && "Op types should be identical!"); 02109 return getCompareTy(pred, C1, C2); 02110 } 02111 02112 Constant *ConstantExpr::getSelectTy(const Type *ReqTy, Constant *C, 02113 Constant *V1, Constant *V2) { 02114 assert(!SelectInst::areInvalidOperands(C, V1, V2)&&"Invalid select operands"); 02115 02116 if (ReqTy == V1->getType()) 02117 if (Constant *SC = ConstantFoldSelectInstruction(C, V1, V2)) 02118 return SC; // Fold common cases 02119 02120 std::vector<Constant*> argVec(3, C); 02121 argVec[1] = V1; 02122 argVec[2] = V2; 02123 ExprMapKeyType Key(Instruction::Select, argVec); 02124 return ExprConstants->getOrCreate(ReqTy, Key); 02125 } 02126 02127 Constant *ConstantExpr::getGetElementPtrTy(const Type *ReqTy, Constant *C, 02128 Value* const *Idxs, 02129 unsigned NumIdx) { 02130 assert(GetElementPtrInst::getIndexedType(C->getType(), Idxs, 02131 Idxs+NumIdx) == 02132 cast<PointerType>(ReqTy)->getElementType() && 02133 "GEP indices invalid!"); 02134 02135 if (Constant *FC = ConstantFoldGetElementPtr(C, (Constant**)Idxs, NumIdx)) 02136 return FC; // Fold a few common cases... 02137 02138 assert(isa<PointerType>(C->getType()) && 02139 "Non-pointer type for constant GetElementPtr expression"); 02140 // Look up the constant in the table first to ensure uniqueness 02141 std::vector<Constant*> ArgVec; 02142 ArgVec.reserve(NumIdx+1); 02143 ArgVec.push_back(C); 02144 for (unsigned i = 0; i != NumIdx; ++i) 02145 ArgVec.push_back(cast<Constant>(Idxs[i])); 02146 const ExprMapKeyType Key(Instruction::GetElementPtr, ArgVec); 02147 return ExprConstants->getOrCreate(ReqTy, Key); 02148 } 02149 02150 Constant *ConstantExpr::getGetElementPtr(Constant *C, Value* const *Idxs, 02151 unsigned NumIdx) { 02152 // Get the result type of the getelementptr! 02153 const Type *Ty = 02154 GetElementPtrInst::getIndexedType(C->getType(), Idxs, Idxs+NumIdx); 02155 assert(Ty && "GEP indices invalid!"); 02156 unsigned As = cast<PointerType>(C->getType())->getAddressSpace(); 02157 return getGetElementPtrTy(PointerType::get(Ty, As), C, Idxs, NumIdx); 02158 } 02159 02160 Constant *ConstantExpr::getGetElementPtr(Constant *C, Constant* const *Idxs, 02161 unsigned NumIdx) { 02162 return getGetElementPtr(C, (Value* const *)Idxs, NumIdx); 02163 } 02164 02165 02166 Constant * 02167 ConstantExpr::getICmp(unsigned short pred, Constant* LHS, Constant* RHS) { 02168 assert(LHS->getType() == RHS->getType()); 02169 assert(pred >= ICmpInst::FIRST_ICMP_PREDICATE && 02170 pred <= ICmpInst::LAST_ICMP_PREDICATE && "Invalid ICmp Predicate"); 02171 02172 if (Constant *FC = ConstantFoldCompareInstruction(pred, LHS, RHS)) 02173 return FC; // Fold a few common cases... 02174 02175 // Look up the constant in the table first to ensure uniqueness 02176 std::vector<Constant*> ArgVec; 02177 ArgVec.push_back(LHS); 02178 ArgVec.push_back(RHS); 02179 // Get the key type with both the opcode and predicate 02180 const ExprMapKeyType Key(Instruction::ICmp, ArgVec, pred); 02181 return ExprConstants->getOrCreate(Type::Int1Ty, Key); 02182 } 02183 02184 Constant * 02185 ConstantExpr::getFCmp(unsigned short pred, Constant* LHS, Constant* RHS) { 02186 assert(LHS->getType() == RHS->getType()); 02187 assert(pred <= FCmpInst::LAST_FCMP_PREDICATE && "Invalid FCmp Predicate"); 02188 02189 if (Constant *FC = ConstantFoldCompareInstruction(pred, LHS, RHS)) 02190 return FC; // Fold a few common cases... 02191 02192 // Look up the constant in the table first to ensure uniqueness 02193 std::vector<Constant*> ArgVec; 02194 ArgVec.push_back(LHS); 02195 ArgVec.push_back(RHS); 02196 // Get the key type with both the opcode and predicate 02197 const ExprMapKeyType Key(Instruction::FCmp, ArgVec, pred); 02198 return ExprConstants->getOrCreate(Type::Int1Ty, Key); 02199 } 02200 02201 Constant * 02202 ConstantExpr::getVICmp(unsigned short pred, Constant* LHS, Constant* RHS) { 02203 assert(isa<VectorType>(LHS->getType()) && LHS->getType() == RHS->getType() && 02204 "Tried to create vicmp operation on non-vector type!"); 02205 assert(pred >= ICmpInst::FIRST_ICMP_PREDICATE && 02206 pred <= ICmpInst::LAST_ICMP_PREDICATE && "Invalid VICmp Predicate"); 02207 02208 const VectorType *VTy = cast<VectorType>(LHS->getType()); 02209 const Type *EltTy = VTy->getElementType(); 02210 unsigned NumElts = VTy->getNumElements(); 02211 02212 // See if we can fold the element-wise comparison of the LHS and RHS. 02213 SmallVector<Constant *, 16> LHSElts, RHSElts; 02214 LHS->getVectorElements(LHSElts); 02215 RHS->getVectorElements(RHSElts); 02216 02217 if (!LHSElts.empty() && !RHSElts.empty()) { 02218 SmallVector<Constant *, 16> Elts; 02219 for (unsigned i = 0; i != NumElts; ++i) { 02220 Constant *FC = ConstantFoldCompareInstruction(pred, LHSElts[i], 02221 RHSElts[i]); 02222 if (ConstantInt *FCI = dyn_cast_or_null<ConstantInt>(FC)) { 02223 if (FCI->getZExtValue()) 02224 Elts.push_back(ConstantInt::getAllOnesValue(EltTy)); 02225 else 02226 Elts.push_back(ConstantInt::get(EltTy, 0ULL)); 02227 } else if (FC && isa<UndefValue>(FC)) { 02228 Elts.push_back(UndefValue::get(EltTy)); 02229 } else { 02230 break; 02231 } 02232 } 02233 if (Elts.size() == NumElts) 02234 return ConstantVector::get(&Elts[0], Elts.size()); 02235 } 02236 02237 // Look up the constant in the table first to ensure uniqueness 02238 std::vector<Constant*> ArgVec; 02239 ArgVec.push_back(LHS); 02240 ArgVec.push_back(RHS); 02241 // Get the key type with both the opcode and predicate 02242 const ExprMapKeyType Key(Instruction::VICmp, ArgVec, pred); 02243 return ExprConstants->getOrCreate(LHS->getType(), Key); 02244 } 02245 02246 Constant * 02247 ConstantExpr::getVFCmp(unsigned short pred, Constant* LHS, Constant* RHS) { 02248 assert(isa<VectorType>(LHS->getType()) && 02249 "Tried to create vfcmp operation on non-vector type!"); 02250 assert(LHS->getType() == RHS->getType()); 02251 assert(pred <= FCmpInst::LAST_FCMP_PREDICATE && "Invalid VFCmp Predicate"); 02252 02253 const VectorType *VTy = cast<VectorType>(LHS->getType()); 02254 unsigned NumElts = VTy->getNumElements(); 02255 const Type *EltTy = VTy->getElementType(); 02256 const Type *REltTy = IntegerType::get(EltTy->getPrimitiveSizeInBits()); 02257 const Type *ResultTy = VectorType::get(REltTy, NumElts); 02258 02259 // See if we can fold the element-wise comparison of the LHS and RHS. 02260 SmallVector<Constant *, 16> LHSElts, RHSElts; 02261 LHS->getVectorElements(LHSElts); 02262 RHS->getVectorElements(RHSElts); 02263 02264 if (!LHSElts.empty() && !RHSElts.empty()) { 02265 SmallVector<Constant *, 16> Elts; 02266 for (unsigned i = 0; i != NumElts; ++i) { 02267 Constant *FC = ConstantFoldCompareInstruction(pred, LHSElts[i], 02268 RHSElts[i]); 02269 if (ConstantInt *FCI = dyn_cast_or_null<ConstantInt>(FC)) { 02270 if (FCI->getZExtValue()) 02271 Elts.push_back(ConstantInt::getAllOnesValue(REltTy)); 02272 else 02273 Elts.push_back(ConstantInt::get(REltTy, 0ULL)); 02274 } else if (FC && isa<UndefValue>(FC)) { 02275 Elts.push_back(UndefValue::get(REltTy)); 02276 } else { 02277 break; 02278 } 02279 } 02280 if (Elts.size() == NumElts) 02281 return ConstantVector::get(&Elts[0], Elts.size()); 02282 } 02283 02284 // Look up the constant in the table first to ensure uniqueness 02285 std::vector<Constant*> ArgVec; 02286 ArgVec.push_back(LHS); 02287 ArgVec.push_back(RHS); 02288 // Get the key type with both the opcode and predicate 02289 const ExprMapKeyType Key(Instruction::VFCmp, ArgVec, pred); 02290 return ExprConstants->getOrCreate(ResultTy, Key); 02291 } 02292 02293 Constant *ConstantExpr::getExtractElementTy(const Type *ReqTy, Constant *Val, 02294 Constant *Idx) { 02295 if (Constant *FC = ConstantFoldExtractElementInstruction(Val, Idx)) 02296 return FC; // Fold a few common cases... 02297 // Look up the constant in the table first to ensure uniqueness 02298 std::vector<Constant*> ArgVec(1, Val); 02299 ArgVec.push_back(Idx); 02300 const ExprMapKeyType Key(Instruction::ExtractElement,ArgVec); 02301 return ExprConstants->getOrCreate(ReqTy, Key); 02302 } 02303 02304 Constant *ConstantExpr::getExtractElement(Constant *Val, Constant *Idx) { 02305 assert(isa<VectorType>(Val->getType()) && 02306 "Tried to create extractelement operation on non-vector type!"); 02307 assert(Idx->getType() == Type::Int32Ty && 02308 "Extractelement index must be i32 type!"); 02309 return getExtractElementTy(cast<VectorType>(Val->getType())->getElementType(), 02310 Val, Idx); 02311 } 02312 02313 Constant *ConstantExpr::getInsertElementTy(const Type *ReqTy, Constant *Val, 02314 Constant *Elt, Constant *Idx) { 02315 if (Constant *FC = ConstantFoldInsertElementInstruction(Val, Elt, Idx)) 02316 return FC; // Fold a few common cases... 02317 // Look up the constant in the table first to ensure uniqueness 02318 std::vector<Constant*> ArgVec(1, Val); 02319 ArgVec.push_back(Elt); 02320 ArgVec.push_back(Idx); 02321 const ExprMapKeyType Key(Instruction::InsertElement,ArgVec); 02322 return ExprConstants->getOrCreate(ReqTy, Key); 02323 } 02324 02325 Constant *ConstantExpr::getInsertElement(Constant *Val, Constant *Elt, 02326 Constant *Idx) { 02327 assert(isa<VectorType>(Val->getType()) && 02328 "Tried to create insertelement operation on non-vector type!"); 02329 assert(Elt->getType() == cast<VectorType>(Val->getType())->getElementType() 02330 && "Insertelement types must match!"); 02331 assert(Idx->getType() == Type::Int32Ty && 02332 "Insertelement index must be i32 type!"); 02333 return getInsertElementTy(Val->getType(), Val, Elt, Idx); 02334 } 02335 02336 Constant *ConstantExpr::getShuffleVectorTy(const Type *ReqTy, Constant *V1, 02337 Constant *V2, Constant *Mask) { 02338 if (Constant *FC = ConstantFoldShuffleVectorInstruction(V1, V2, Mask)) 02339 return FC; // Fold a few common cases... 02340 // Look up the constant in the table first to ensure uniqueness 02341 std::vector<Constant*> ArgVec(1, V1); 02342 ArgVec.push_back(V2); 02343 ArgVec.push_back(Mask); 02344 const ExprMapKeyType Key(Instruction::ShuffleVector,ArgVec); 02345 return ExprConstants->getOrCreate(ReqTy, Key); 02346 } 02347 02348 Constant *ConstantExpr::getShuffleVector(Constant *V1, Constant *V2, 02349 Constant *Mask) { 02350 assert(ShuffleVectorInst::isValidOperands(V1, V2, Mask) && 02351 "Invalid shuffle vector constant expr operands!"); 02352 return getShuffleVectorTy(V1->getType(), V1, V2, Mask); 02353 } 02354 02355 Constant *ConstantExpr::getInsertValueTy(const Type *ReqTy, Constant *Agg, 02356 Constant *Val, 02357 const unsigned *Idxs, unsigned NumIdx) { 02358 assert(ExtractValueInst::getIndexedType(Agg->getType(), Idxs, 02359 Idxs+NumIdx) == Val->getType() && 02360 "insertvalue indices invalid!"); 02361 assert(Agg->getType() == ReqTy && 02362 "insertvalue type invalid!"); 02363 assert(Agg->getType()->isFirstClassType() && 02364 "Non-first-class type for constant InsertValue expression"); 02365 Constant *FC = ConstantFoldInsertValueInstruction(Agg, Val, Idxs, NumIdx); 02366 assert(FC && "InsertValue constant expr couldn't be folded!"); 02367 return FC; 02368 } 02369 02370 Constant *ConstantExpr::getInsertValue(Constant *Agg, Constant *Val, 02371 const unsigned *IdxList, unsigned NumIdx) { 02372 assert(Agg->getType()->isFirstClassType() && 02373 "Tried to create insertelement operation on non-first-class type!"); 02374 02375 const Type *ReqTy = Agg->getType(); 02376 #ifndef NDEBUG 02377 const Type *ValTy = 02378 ExtractValueInst::getIndexedType(Agg->getType(), IdxList, IdxList+NumIdx); 02379 #endif 02380 assert(ValTy == Val->getType() && "insertvalue indices invalid!"); 02381 return getInsertValueTy(ReqTy, Agg, Val, IdxList, NumIdx); 02382 } 02383 02384 Constant *ConstantExpr::getExtractValueTy(const Type *ReqTy, Constant *Agg, 02385 const unsigned *Idxs, unsigned NumIdx) { 02386 assert(ExtractValueInst::getIndexedType(Agg->getType(), Idxs, 02387 Idxs+NumIdx) == ReqTy && 02388 "extractvalue indices invalid!"); 02389 assert(Agg->getType()->isFirstClassType() && 02390 "Non-first-class type for constant extractvalue expression"); 02391 Constant *FC = ConstantFoldExtractValueInstruction(Agg, Idxs, NumIdx); 02392 assert(FC && "ExtractValue constant expr couldn't be folded!"); 02393 return FC; 02394 } 02395 02396 Constant *ConstantExpr::getExtractValue(Constant *Agg, 02397 const unsigned *IdxList, unsigned NumIdx) { 02398 assert(Agg->getType()->isFirstClassType() && 02399 "Tried to create extractelement operation on non-first-class type!"); 02400 02401 const Type *ReqTy = 02402 ExtractValueInst::getIndexedType(Agg->getType(), IdxList, IdxList+NumIdx); 02403 assert(ReqTy && "extractvalue indices invalid!"); 02404 return getExtractValueTy(ReqTy, Agg, IdxList, NumIdx); 02405 } 02406 02407 Constant *ConstantExpr::getZeroValueForNegationExpr(const Type *Ty) { 02408 if (const VectorType *PTy = dyn_cast<VectorType>(Ty)) 02409 if (PTy->getElementType()->isFloatingPoint()) { 02410 std::vector<Constant*> zeros(PTy->getNumElements(), 02411 ConstantFP::getNegativeZero(PTy->getElementType())); 02412 return ConstantVector::get(PTy, zeros); 02413 } 02414 02415 if (Ty->isFloatingPoint()) 02416 return ConstantFP::getNegativeZero(Ty); 02417 02418 return Constant::getNullValue(Ty); 02419 } 02420 02421 // destroyConstant - Remove the constant from the constant table... 02422 // 02423 void ConstantExpr::destroyConstant() { 02424 ExprConstants->remove(this); 02425 destroyConstantImpl(); 02426 } 02427 02428 const char *ConstantExpr::getOpcodeName() const { 02429 return Instruction::getOpcodeName(getOpcode()); 02430 } 02431 02432 //===----------------------------------------------------------------------===// 02433 // replaceUsesOfWithOnConstant implementations 02434 02435 /// replaceUsesOfWithOnConstant - Update this constant array to change uses of 02436 /// 'From' to be uses of 'To'. This must update the uniquing data structures 02437 /// etc. 02438 /// 02439 /// Note that we intentionally replace all uses of From with To here. Consider 02440 /// a large array that uses 'From' 1000 times. By handling this case all here, 02441 /// ConstantArray::replaceUsesOfWithOnConstant is only invoked once, and that 02442 /// single invocation handles all 1000 uses. Handling them one at a time would 02443 /// work, but would be really slow because it would have to unique each updated 02444 /// array instance. 02445 void ConstantArray::replaceUsesOfWithOnConstant(Value *From, Value *To, 02446 Use *U) { 02447 assert(isa<Constant>(To) && "Cannot make Constant refer to non-constant!"); 02448 Constant *ToC = cast<Constant>(To); 02449 02450 std::pair<ArrayConstantsTy::MapKey, Constant*> Lookup; 02451 Lookup.first.first = getType(); 02452 Lookup.second = this; 02453 02454 std::vector<Constant*> &Values = Lookup.first.second; 02455 Values.reserve(getNumOperands()); // Build replacement array. 02456 02457 // Fill values with the modified operands of the constant array. Also, 02458 // compute whether this turns into an all-zeros array. 02459 bool isAllZeros = false; 02460 unsigned NumUpdated = 0; 02461 if (!ToC->isNullValue()) { 02462 for (Use *O = OperandList, *E = OperandList+getNumOperands(); O != E; ++O) { 02463 Constant *Val = cast<Constant>(O->get()); 02464 if (Val == From) { 02465 Val = ToC; 02466 ++NumUpdated; 02467 } 02468 Values.push_back(Val); 02469 } 02470 } else { 02471 isAllZeros = true; 02472 for (Use *O = OperandList, *E = OperandList+getNumOperands(); O != E; ++O) { 02473 Constant *Val = cast<Constant>(O->get()); 02474 if (Val == From) { 02475 Val = ToC; 02476 ++NumUpdated; 02477 } 02478 Values.push_back(Val); 02479 if (isAllZeros) isAllZeros = Val->isNullValue(); 02480 } 02481 } 02482 02483 Constant *Replacement = 0; 02484 if (isAllZeros) { 02485 Replacement = ConstantAggregateZero::get(getType()); 02486 } else { 02487 // Check to see if we have this array type already. 02488 bool Exists; 02489 ArrayConstantsTy::MapTy::iterator I = 02490 ArrayConstants->InsertOrGetItem(Lookup, Exists); 02491 02492 if (Exists) { 02493 Replacement = I->second; 02494 } else { 02495 // Okay, the new shape doesn't exist in the system yet. Instead of 02496 // creating a new constant array, inserting it, replaceallusesof'ing the 02497 // old with the new, then deleting the old... just update the current one 02498 // in place! 02499 ArrayConstants->MoveConstantToNewSlot(this, I); 02500 02501 // Update to the new value. Optimize for the case when we have a single 02502 // operand that we're changing, but handle bulk updates efficiently. 02503 if (NumUpdated == 1) { 02504 unsigned OperandToUpdate = U-OperandList; 02505 assert(getOperand(OperandToUpdate) == From && 02506 "ReplaceAllUsesWith broken!"); 02507 setOperand(OperandToUpdate, ToC); 02508 } else { 02509 for (unsigned i = 0, e = getNumOperands(); i != e; ++i) 02510 if (getOperand(i) == From) 02511 setOperand(i, ToC); 02512 } 02513 return; 02514 } 02515 } 02516 02517 // Otherwise, I do need to replace this with an existing value. 02518 assert(Replacement != this && "I didn't contain From!"); 02519 02520 // Everyone using this now uses the replacement. 02521 uncheckedReplaceAllUsesWith(Replacement); 02522 02523 // Delete the old constant! 02524 destroyConstant(); 02525 } 02526 02527 void ConstantStruct::replaceUsesOfWithOnConstant(Value *From, Value *To, 02528 Use *U) { 02529 assert(isa<Constant>(To) && "Cannot make Constant refer to non-constant!"); 02530 Constant *ToC = cast<Constant>(To); 02531 02532 unsigned OperandToUpdate = U-OperandList; 02533 assert(getOperand(OperandToUpdate) == From && "ReplaceAllUsesWith broken!"); 02534 02535 std::pair<StructConstantsTy::MapKey, Constant*> Lookup; 02536 Lookup.first.first = getType(); 02537 Lookup.second = this; 02538 std::vector<Constant*> &Values = Lookup.first.second; 02539 Values.reserve(getNumOperands()); // Build replacement struct. 02540 02541 02542 // Fill values with the modified operands of the constant struct. Also, 02543 // compute whether this turns into an all-zeros struct. 02544 bool isAllZeros = false; 02545 if (!ToC->isNullValue()) { 02546 for (Use *O = OperandList, *E = OperandList+getNumOperands(); O != E; ++O) 02547 Values.push_back(cast<Constant>(O->get())); 02548 } else { 02549 isAllZeros = true; 02550 for (Use *O = OperandList, *E = OperandList+getNumOperands(); O != E; ++O) { 02551 Constant *Val = cast<Constant>(O->get()); 02552 Values.push_back(Val); 02553 if (isAllZeros) isAllZeros = Val->isNullValue(); 02554 } 02555 } 02556 Values[OperandToUpdate] = ToC; 02557 02558 Constant *Replacement = 0; 02559 if (isAllZeros) { 02560 Replacement = ConstantAggregateZero::get(getType()); 02561 } else { 02562 // Check to see if we have this array type already. 02563 bool Exists; 02564 StructConstantsTy::MapTy::iterator I = 02565 StructConstants->InsertOrGetItem(Lookup, Exists); 02566 02567 if (Exists) { 02568 Replacement = I->second; 02569 } else { 02570 // Okay, the new shape doesn't exist in the system yet. Instead of 02571 // creating a new constant struct, inserting it, replaceallusesof'ing the 02572 // old with the new, then deleting the old... just update the current one 02573 // in place! 02574 StructConstants->MoveConstantToNewSlot(this, I); 02575 02576 // Update to the new value. 02577 setOperand(OperandToUpdate, ToC); 02578 return; 02579 } 02580 } 02581 02582 assert(Replacement != this && "I didn't contain From!"); 02583 02584 // Everyone using this now uses the replacement. 02585 uncheckedReplaceAllUsesWith(Replacement); 02586 02587 // Delete the old constant! 02588 destroyConstant(); 02589 } 02590 02591 void ConstantVector::replaceUsesOfWithOnConstant(Value *From, Value *To, 02592 Use *U) { 02593 assert(isa<Constant>(To) && "Cannot make Constant refer to non-constant!"); 02594 02595 std::vector<Constant*> Values; 02596 Values.reserve(getNumOperands()); // Build replacement array... 02597 for (unsigned i = 0, e = getNumOperands(); i != e; ++i) { 02598 Constant *Val = getOperand(i); 02599 if (Val == From) Val = cast<Constant>(To); 02600 Values.push_back(Val); 02601 } 02602 02603 Constant *Replacement = ConstantVector::get(getType(), Values); 02604 assert(Replacement != this && "I didn't contain From!"); 02605 02606 // Everyone using this now uses the replacement. 02607 uncheckedReplaceAllUsesWith(Replacement); 02608 02609 // Delete the old constant! 02610 destroyConstant(); 02611 } 02612 02613 void ConstantExpr::replaceUsesOfWithOnConstant(Value *From, Value *ToV, 02614 Use *U) { 02615 assert(isa<Constant>(ToV) && "Cannot make Constant refer to non-constant!"); 02616 Constant *To = cast<Constant>(ToV); 02617 02618 Constant *Replacement = 0; 02619 if (getOpcode() == Instruction::GetElementPtr) { 02620 SmallVector<Constant*, 8> Indices; 02621 Constant *Pointer = getOperand(0); 02622 Indices.reserve(getNumOperands()-1); 02623 if (Pointer == From) Pointer = To; 02624 02625 for (unsigned i = 1, e = getNumOperands(); i != e; ++i) { 02626 Constant *Val = getOperand(i); 02627 if (Val == From) Val = To; 02628 Indices.push_back(Val); 02629 } 02630 Replacement = ConstantExpr::getGetElementPtr(Pointer, 02631 &Indices[0], Indices.size()); 02632 } else if (getOpcode() == Instruction::ExtractValue) { 02633 Constant *Agg = getOperand(0); 02634 if (Agg == From) Agg = To; 02635 02636 const SmallVector<unsigned, 4> &Indices = getIndices(); 02637 Replacement = ConstantExpr::getExtractValue(Agg, 02638 &Indices[0], Indices.size()); 02639 } else if (getOpcode() == Instruction::InsertValue) { 02640 Constant *Agg = getOperand(0); 02641 Constant *Val = getOperand(1); 02642 if (Agg == From) Agg = To; 02643 if (Val == From) Val = To; 02644 02645 const SmallVector<unsigned, 4> &Indices = getIndices(); 02646 Replacement = ConstantExpr::getInsertValue(Agg, Val, 02647 &Indices[0], Indices.size()); 02648 } else if (isCast()) { 02649 assert(getOperand(0) == From && "Cast only has one use!"); 02650 Replacement = ConstantExpr::getCast(getOpcode(), To, getType()); 02651 } else if (getOpcode() == Instruction::Select) { 02652 Constant *C1 = getOperand(0); 02653 Constant *C2 = getOperand(1); 02654 Constant *C3 = getOperand(2); 02655 if (C1 == From) C1 = To; 02656 if (C2 == From) C2 = To; 02657 if (C3 == From) C3 = To; 02658 Replacement = ConstantExpr::getSelect(C1, C2, C3); 02659 } else if (getOpcode() == Instruction::ExtractElement) { 02660 Constant *C1 = getOperand(0); 02661 Constant *C2 = getOperand(1); 02662 if (C1 == From) C1 = To; 02663 if (C2 == From) C2 = To; 02664 Replacement = ConstantExpr::getExtractElement(C1, C2); 02665 } else if (getOpcode() == Instruction::InsertElement) { 02666 Constant *C1 = getOperand(0); 02667 Constant *C2 = getOperand(1); 02668 Constant *C3 = getOperand(1); 02669 if (C1 == From) C1 = To; 02670 if (C2 == From) C2 = To; 02671 if (C3 == From) C3 = To; 02672 Replacement = ConstantExpr::getInsertElement(C1, C2, C3); 02673 } else if (getOpcode() == Instruction::ShuffleVector) { 02674 Constant *C1 = getOperand(0); 02675 Constant *C2 = getOperand(1); 02676 Constant *C3 = getOperand(2); 02677 if (C1 == From) C1 = To; 02678 if (C2 == From) C2 = To; 02679 if (C3 == From) C3 = To; 02680 Replacement = ConstantExpr::getShuffleVector(C1, C2, C3); 02681 } else if (isCompare()) { 02682 Constant *C1 = getOperand(0); 02683 Constant *C2 = getOperand(1); 02684 if (C1 == From) C1 = To; 02685 if (C2 == From) C2 = To; 02686 if (getOpcode() == Instruction::ICmp) 02687 Replacement = ConstantExpr::getICmp(getPredicate(), C1, C2); 02688 else if (getOpcode() == Instruction::FCmp) 02689 Replacement = ConstantExpr::getFCmp(getPredicate(), C1, C2); 02690 else if (getOpcode() == Instruction::VICmp) 02691 Replacement = ConstantExpr::getVICmp(getPredicate(), C1, C2); 02692 else { 02693 assert(getOpcode() == Instruction::VFCmp); 02694 Replacement = ConstantExpr::getVFCmp(getPredicate(), C1, C2); 02695 } 02696 } else if (getNumOperands() == 2) { 02697 Constant *C1 = getOperand(0); 02698 Constant *C2 = getOperand(1); 02699 if (C1 == From) C1 = To; 02700 if (C2 == From) C2 = To; 02701 Replacement = ConstantExpr::get(getOpcode(), C1, C2); 02702 } else { 02703 assert(0 && "Unknown ConstantExpr type!"); 02704 return; 02705 } 02706 02707 assert(Replacement != this && "I didn't contain From!"); 02708 02709 // Everyone using this now uses the replacement. 02710 uncheckedReplaceAllUsesWith(Replacement); 02711 02712 // Delete the old constant! 02713 destroyConstant(); 02714 }