Chris Lattner | 2b383d2e | 2003-05-13 21:37:02 +0000 | [diff] [blame] | 1 | //===-- Constants.cpp - Implement Constant nodes --------------------------===// |
Misha Brukman | b1c9317 | 2005-04-21 23:48:37 +0000 | [diff] [blame] | 2 | // |
John Criswell | 482202a | 2003-10-20 19:43:21 +0000 | [diff] [blame] | 3 | // The LLVM Compiler Infrastructure |
| 4 | // |
Chris Lattner | f3ebc3f | 2007-12-29 20:36:04 +0000 | [diff] [blame] | 5 | // This file is distributed under the University of Illinois Open Source |
| 6 | // License. See LICENSE.TXT for details. |
Misha Brukman | b1c9317 | 2005-04-21 23:48:37 +0000 | [diff] [blame] | 7 | // |
John Criswell | 482202a | 2003-10-20 19:43:21 +0000 | [diff] [blame] | 8 | //===----------------------------------------------------------------------===// |
Chris Lattner | 2f7c963 | 2001-06-06 20:29:01 +0000 | [diff] [blame] | 9 | // |
Chris Lattner | 3462ae3 | 2001-12-03 22:26:30 +0000 | [diff] [blame] | 10 | // This file implements the Constant* classes... |
Chris Lattner | 2f7c963 | 2001-06-06 20:29:01 +0000 | [diff] [blame] | 11 | // |
| 12 | //===----------------------------------------------------------------------===// |
| 13 | |
Owen Anderson | edb4a70 | 2009-07-24 23:12:02 +0000 | [diff] [blame] | 14 | #include "LLVMContextImpl.h" |
Chris Lattner | ca14237 | 2002-04-28 19:55:58 +0000 | [diff] [blame] | 15 | #include "llvm/Constants.h" |
Chris Lattner | 33e93b8 | 2007-02-27 03:05:06 +0000 | [diff] [blame] | 16 | #include "ConstantFold.h" |
Chris Lattner | 2f7c963 | 2001-06-06 20:29:01 +0000 | [diff] [blame] | 17 | #include "llvm/DerivedTypes.h" |
Reid Spencer | 1ebe1ab | 2004-07-17 23:48:33 +0000 | [diff] [blame] | 18 | #include "llvm/GlobalValue.h" |
Misha Brukman | 63b38bd | 2004-07-29 17:30:56 +0000 | [diff] [blame] | 19 | #include "llvm/Instructions.h" |
Chris Lattner | d7a7330 | 2001-10-13 06:57:33 +0000 | [diff] [blame] | 20 | #include "llvm/Module.h" |
Dan Gohman | 7d82e13 | 2009-07-18 01:49:22 +0000 | [diff] [blame] | 21 | #include "llvm/Operator.h" |
Nick Lewycky | 49f8919 | 2009-04-04 07:22:01 +0000 | [diff] [blame] | 22 | #include "llvm/ADT/FoldingSet.h" |
Reid Spencer | 7c16caa | 2004-09-01 22:55:40 +0000 | [diff] [blame] | 23 | #include "llvm/ADT/StringExtras.h" |
Nick Lewycky | 49f8919 | 2009-04-04 07:22:01 +0000 | [diff] [blame] | 24 | #include "llvm/ADT/StringMap.h" |
Chris Lattner | 3d27be1 | 2006-08-27 12:54:02 +0000 | [diff] [blame] | 25 | #include "llvm/Support/Compiler.h" |
Bill Wendling | 6a462f1 | 2006-11-17 08:03:48 +0000 | [diff] [blame] | 26 | #include "llvm/Support/Debug.h" |
Torok Edwin | ccb29cd | 2009-07-11 13:10:19 +0000 | [diff] [blame] | 27 | #include "llvm/Support/ErrorHandling.h" |
Chris Lattner | 69edc98 | 2006-09-28 00:35:06 +0000 | [diff] [blame] | 28 | #include "llvm/Support/ManagedStatic.h" |
Bill Wendling | 6a462f1 | 2006-11-17 08:03:48 +0000 | [diff] [blame] | 29 | #include "llvm/Support/MathExtras.h" |
Owen Anderson | 0d2de8c | 2009-06-20 00:24:58 +0000 | [diff] [blame] | 30 | #include "llvm/System/Mutex.h" |
Owen Anderson | 2d7231d | 2009-06-17 18:40:29 +0000 | [diff] [blame] | 31 | #include "llvm/System/RWMutex.h" |
Owen Anderson | 7d42b95 | 2009-06-18 16:54:52 +0000 | [diff] [blame] | 32 | #include "llvm/System/Threading.h" |
Chris Lattner | a80bf0b | 2007-02-20 06:39:57 +0000 | [diff] [blame] | 33 | #include "llvm/ADT/DenseMap.h" |
Chris Lattner | b5d7030 | 2007-02-19 20:01:23 +0000 | [diff] [blame] | 34 | #include "llvm/ADT/SmallVector.h" |
Chris Lattner | 2f7c963 | 2001-06-06 20:29:01 +0000 | [diff] [blame] | 35 | #include <algorithm> |
Reid Spencer | 3aaaa0b | 2007-02-05 20:47:22 +0000 | [diff] [blame] | 36 | #include <map> |
Chris Lattner | 189d19f | 2003-11-21 20:23:48 +0000 | [diff] [blame] | 37 | using namespace llvm; |
Brian Gaeke | 960707c | 2003-11-11 22:41:34 +0000 | [diff] [blame] | 38 | |
Chris Lattner | 2f7c963 | 2001-06-06 20:29:01 +0000 | [diff] [blame] | 39 | //===----------------------------------------------------------------------===// |
Chris Lattner | 3462ae3 | 2001-12-03 22:26:30 +0000 | [diff] [blame] | 40 | // Constant Class |
Chris Lattner | 2f7c963 | 2001-06-06 20:29:01 +0000 | [diff] [blame] | 41 | //===----------------------------------------------------------------------===// |
| 42 | |
Owen Anderson | 5a1acd9 | 2009-07-31 20:28:14 +0000 | [diff] [blame] | 43 | // Constructor to create a '0' constant of arbitrary type... |
| 44 | static const uint64_t zero[2] = {0, 0}; |
| 45 | Constant* Constant::getNullValue(const Type* Ty) { |
| 46 | switch (Ty->getTypeID()) { |
| 47 | case Type::IntegerTyID: |
| 48 | return ConstantInt::get(Ty, 0); |
| 49 | case Type::FloatTyID: |
| 50 | return ConstantFP::get(Ty->getContext(), APFloat(APInt(32, 0))); |
| 51 | case Type::DoubleTyID: |
| 52 | return ConstantFP::get(Ty->getContext(), APFloat(APInt(64, 0))); |
| 53 | case Type::X86_FP80TyID: |
| 54 | return ConstantFP::get(Ty->getContext(), APFloat(APInt(80, 2, zero))); |
| 55 | case Type::FP128TyID: |
| 56 | return ConstantFP::get(Ty->getContext(), |
| 57 | APFloat(APInt(128, 2, zero), true)); |
| 58 | case Type::PPC_FP128TyID: |
| 59 | return ConstantFP::get(Ty->getContext(), APFloat(APInt(128, 2, zero))); |
| 60 | case Type::PointerTyID: |
| 61 | return ConstantPointerNull::get(cast<PointerType>(Ty)); |
| 62 | case Type::StructTyID: |
| 63 | case Type::ArrayTyID: |
| 64 | case Type::VectorTyID: |
| 65 | return ConstantAggregateZero::get(Ty); |
| 66 | default: |
| 67 | // Function, Label, or Opaque type? |
| 68 | assert(!"Cannot create a null constant of that type!"); |
| 69 | return 0; |
| 70 | } |
| 71 | } |
| 72 | |
Dan Gohman | f011f5a | 2009-08-03 22:07:33 +0000 | [diff] [blame] | 73 | Constant* Constant::getIntegerValue(const Type* Ty, const APInt &V) { |
| 74 | const Type *ScalarTy = Ty->getScalarType(); |
| 75 | |
| 76 | // Create the base integer constant. |
| 77 | Constant *C = ConstantInt::get(Ty->getContext(), V); |
| 78 | |
| 79 | // Convert an integer to a pointer, if necessary. |
| 80 | if (const PointerType *PTy = dyn_cast<PointerType>(ScalarTy)) |
| 81 | C = ConstantExpr::getIntToPtr(C, PTy); |
| 82 | |
| 83 | // Broadcast a scalar to a vector, if necessary. |
| 84 | if (const VectorType *VTy = dyn_cast<VectorType>(Ty)) |
| 85 | C = ConstantVector::get(std::vector<Constant *>(VTy->getNumElements(), C)); |
| 86 | |
| 87 | return C; |
| 88 | } |
| 89 | |
Owen Anderson | 5a1acd9 | 2009-07-31 20:28:14 +0000 | [diff] [blame] | 90 | Constant* Constant::getAllOnesValue(const Type* Ty) { |
| 91 | if (const IntegerType* ITy = dyn_cast<IntegerType>(Ty)) |
| 92 | return ConstantInt::get(Ty->getContext(), |
| 93 | APInt::getAllOnesValue(ITy->getBitWidth())); |
| 94 | |
| 95 | std::vector<Constant*> Elts; |
| 96 | const VectorType* VTy = cast<VectorType>(Ty); |
| 97 | Elts.resize(VTy->getNumElements(), getAllOnesValue(VTy->getElementType())); |
| 98 | assert(Elts[0] && "Not a vector integer type!"); |
| 99 | return cast<ConstantVector>(ConstantVector::get(Elts)); |
| 100 | } |
| 101 | |
Chris Lattner | 3462ae3 | 2001-12-03 22:26:30 +0000 | [diff] [blame] | 102 | void Constant::destroyConstantImpl() { |
| 103 | // When a Constant is destroyed, there may be lingering |
Chris Lattner | d7a7330 | 2001-10-13 06:57:33 +0000 | [diff] [blame] | 104 | // references to the constant by other constants in the constant pool. These |
Misha Brukman | be372b9 | 2003-08-21 22:14:26 +0000 | [diff] [blame] | 105 | // constants are implicitly dependent on the module that is being deleted, |
Chris Lattner | d7a7330 | 2001-10-13 06:57:33 +0000 | [diff] [blame] | 106 | // but they don't know that. Because we only find out when the CPV is |
| 107 | // deleted, we must now notify all of our users (that should only be |
Chris Lattner | 3462ae3 | 2001-12-03 22:26:30 +0000 | [diff] [blame] | 108 | // Constants) that they are, in fact, invalid now and should be deleted. |
Chris Lattner | d7a7330 | 2001-10-13 06:57:33 +0000 | [diff] [blame] | 109 | // |
| 110 | while (!use_empty()) { |
| 111 | Value *V = use_back(); |
| 112 | #ifndef NDEBUG // Only in -g mode... |
Chris Lattner | d9f4ac66 | 2002-07-18 00:14:50 +0000 | [diff] [blame] | 113 | if (!isa<Constant>(V)) |
Bill Wendling | 6a462f1 | 2006-11-17 08:03:48 +0000 | [diff] [blame] | 114 | DOUT << "While deleting: " << *this |
| 115 | << "\n\nUse still stuck around after Def is destroyed: " |
| 116 | << *V << "\n\n"; |
Chris Lattner | d7a7330 | 2001-10-13 06:57:33 +0000 | [diff] [blame] | 117 | #endif |
Vikram S. Adve | 4e537b2 | 2002-07-14 23:13:17 +0000 | [diff] [blame] | 118 | assert(isa<Constant>(V) && "References remain to Constant being destroyed"); |
Reid Spencer | 1ebe1ab | 2004-07-17 23:48:33 +0000 | [diff] [blame] | 119 | Constant *CV = cast<Constant>(V); |
| 120 | CV->destroyConstant(); |
Chris Lattner | d7a7330 | 2001-10-13 06:57:33 +0000 | [diff] [blame] | 121 | |
| 122 | // The constant should remove itself from our use list... |
Vikram S. Adve | 4e537b2 | 2002-07-14 23:13:17 +0000 | [diff] [blame] | 123 | assert((use_empty() || use_back() != V) && "Constant not removed!"); |
Chris Lattner | d7a7330 | 2001-10-13 06:57:33 +0000 | [diff] [blame] | 124 | } |
| 125 | |
| 126 | // Value has no outstanding references it is safe to delete it now... |
| 127 | delete this; |
Chris Lattner | 3856934 | 2001-10-01 20:11:19 +0000 | [diff] [blame] | 128 | } |
Chris Lattner | 2f7c963 | 2001-06-06 20:29:01 +0000 | [diff] [blame] | 129 | |
Chris Lattner | 23dd1f6 | 2006-10-20 00:27:06 +0000 | [diff] [blame] | 130 | /// canTrap - Return true if evaluation of this constant could trap. This is |
| 131 | /// true for things like constant expressions that could divide by zero. |
| 132 | bool Constant::canTrap() const { |
| 133 | assert(getType()->isFirstClassType() && "Cannot evaluate aggregate vals!"); |
| 134 | // The only thing that could possibly trap are constant exprs. |
| 135 | const ConstantExpr *CE = dyn_cast<ConstantExpr>(this); |
| 136 | if (!CE) return false; |
| 137 | |
| 138 | // ConstantExpr traps if any operands can trap. |
| 139 | for (unsigned i = 0, e = getNumOperands(); i != e; ++i) |
| 140 | if (getOperand(i)->canTrap()) |
| 141 | return true; |
| 142 | |
| 143 | // Otherwise, only specific operations can trap. |
| 144 | switch (CE->getOpcode()) { |
| 145 | default: |
| 146 | return false; |
Reid Spencer | 7e80b0b | 2006-10-26 06:15:43 +0000 | [diff] [blame] | 147 | case Instruction::UDiv: |
| 148 | case Instruction::SDiv: |
| 149 | case Instruction::FDiv: |
Reid Spencer | 7eb55b3 | 2006-11-02 01:53:59 +0000 | [diff] [blame] | 150 | case Instruction::URem: |
| 151 | case Instruction::SRem: |
| 152 | case Instruction::FRem: |
Chris Lattner | 23dd1f6 | 2006-10-20 00:27:06 +0000 | [diff] [blame] | 153 | // Div and rem can trap if the RHS is not known to be non-zero. |
| 154 | if (!isa<ConstantInt>(getOperand(1)) || getOperand(1)->isNullValue()) |
| 155 | return true; |
| 156 | return false; |
| 157 | } |
| 158 | } |
| 159 | |
Chris Lattner | 4565ef5 | 2009-07-22 00:05:44 +0000 | [diff] [blame] | 160 | |
| 161 | /// getRelocationInfo - This method classifies the entry according to |
| 162 | /// whether or not it may generate a relocation entry. This must be |
| 163 | /// conservative, so if it might codegen to a relocatable entry, it should say |
| 164 | /// so. The return values are: |
| 165 | /// |
Chris Lattner | 5cd4dd3 | 2009-07-24 03:27:21 +0000 | [diff] [blame] | 166 | /// NoRelocation: This constant pool entry is guaranteed to never have a |
| 167 | /// relocation applied to it (because it holds a simple constant like |
| 168 | /// '4'). |
| 169 | /// LocalRelocation: This entry has relocations, but the entries are |
| 170 | /// guaranteed to be resolvable by the static linker, so the dynamic |
| 171 | /// linker will never see them. |
| 172 | /// GlobalRelocations: This entry may have arbitrary relocations. |
Chris Lattner | 4565ef5 | 2009-07-22 00:05:44 +0000 | [diff] [blame] | 173 | /// |
| 174 | /// FIXME: This really should not be in VMCore. |
Chris Lattner | 5cd4dd3 | 2009-07-24 03:27:21 +0000 | [diff] [blame] | 175 | Constant::PossibleRelocationsTy Constant::getRelocationInfo() const { |
| 176 | if (const GlobalValue *GV = dyn_cast<GlobalValue>(this)) { |
Chris Lattner | 4565ef5 | 2009-07-22 00:05:44 +0000 | [diff] [blame] | 177 | if (GV->hasLocalLinkage() || GV->hasHiddenVisibility()) |
Chris Lattner | 5cd4dd3 | 2009-07-24 03:27:21 +0000 | [diff] [blame] | 178 | return LocalRelocation; // Local to this file/library. |
| 179 | return GlobalRelocations; // Global reference. |
Anton Korobeynikov | 7437b59 | 2009-03-29 17:13:18 +0000 | [diff] [blame] | 180 | } |
Chris Lattner | 4565ef5 | 2009-07-22 00:05:44 +0000 | [diff] [blame] | 181 | |
Chris Lattner | 5cd4dd3 | 2009-07-24 03:27:21 +0000 | [diff] [blame] | 182 | PossibleRelocationsTy Result = NoRelocation; |
Evan Cheng | f9e003b | 2007-03-08 00:59:12 +0000 | [diff] [blame] | 183 | for (unsigned i = 0, e = getNumOperands(); i != e; ++i) |
Chris Lattner | 4565ef5 | 2009-07-22 00:05:44 +0000 | [diff] [blame] | 184 | Result = std::max(Result, getOperand(i)->getRelocationInfo()); |
| 185 | |
| 186 | return Result; |
Evan Cheng | f9e003b | 2007-03-08 00:59:12 +0000 | [diff] [blame] | 187 | } |
| 188 | |
Chris Lattner | 4565ef5 | 2009-07-22 00:05:44 +0000 | [diff] [blame] | 189 | |
Chris Lattner | 2105d66 | 2008-07-10 00:28:11 +0000 | [diff] [blame] | 190 | /// getVectorElements - This method, which is only valid on constant of vector |
| 191 | /// type, returns the elements of the vector in the specified smallvector. |
Chris Lattner | c5098a2 | 2008-07-14 05:10:41 +0000 | [diff] [blame] | 192 | /// This handles breaking down a vector undef into undef elements, etc. For |
| 193 | /// constant exprs and other cases we can't handle, we return an empty vector. |
Owen Anderson | 53a5221 | 2009-07-13 04:09:18 +0000 | [diff] [blame] | 194 | void Constant::getVectorElements(LLVMContext &Context, |
| 195 | SmallVectorImpl<Constant*> &Elts) const { |
Chris Lattner | 2105d66 | 2008-07-10 00:28:11 +0000 | [diff] [blame] | 196 | assert(isa<VectorType>(getType()) && "Not a vector constant!"); |
| 197 | |
| 198 | if (const ConstantVector *CV = dyn_cast<ConstantVector>(this)) { |
| 199 | for (unsigned i = 0, e = CV->getNumOperands(); i != e; ++i) |
| 200 | Elts.push_back(CV->getOperand(i)); |
| 201 | return; |
| 202 | } |
| 203 | |
| 204 | const VectorType *VT = cast<VectorType>(getType()); |
| 205 | if (isa<ConstantAggregateZero>(this)) { |
| 206 | Elts.assign(VT->getNumElements(), |
Owen Anderson | 5a1acd9 | 2009-07-31 20:28:14 +0000 | [diff] [blame] | 207 | Constant::getNullValue(VT->getElementType())); |
Chris Lattner | 2105d66 | 2008-07-10 00:28:11 +0000 | [diff] [blame] | 208 | return; |
| 209 | } |
| 210 | |
Chris Lattner | c5098a2 | 2008-07-14 05:10:41 +0000 | [diff] [blame] | 211 | if (isa<UndefValue>(this)) { |
Owen Anderson | b292b8c | 2009-07-30 23:03:37 +0000 | [diff] [blame] | 212 | Elts.assign(VT->getNumElements(), UndefValue::get(VT->getElementType())); |
Chris Lattner | c5098a2 | 2008-07-14 05:10:41 +0000 | [diff] [blame] | 213 | return; |
| 214 | } |
| 215 | |
| 216 | // Unknown type, must be constant expr etc. |
Chris Lattner | 2105d66 | 2008-07-10 00:28:11 +0000 | [diff] [blame] | 217 | } |
| 218 | |
| 219 | |
| 220 | |
Chris Lattner | 2f7c963 | 2001-06-06 20:29:01 +0000 | [diff] [blame] | 221 | //===----------------------------------------------------------------------===// |
Chris Lattner | a80bf0b | 2007-02-20 06:39:57 +0000 | [diff] [blame] | 222 | // ConstantInt |
Chris Lattner | 2f7c963 | 2001-06-06 20:29:01 +0000 | [diff] [blame] | 223 | //===----------------------------------------------------------------------===// |
| 224 | |
Reid Spencer | b31bffe | 2007-02-26 23:54:03 +0000 | [diff] [blame] | 225 | ConstantInt::ConstantInt(const IntegerType *Ty, const APInt& V) |
Chris Lattner | 5db2f47 | 2007-02-20 05:55:46 +0000 | [diff] [blame] | 226 | : Constant(Ty, ConstantIntVal, 0, 0), Val(V) { |
Reid Spencer | b31bffe | 2007-02-26 23:54:03 +0000 | [diff] [blame] | 227 | assert(V.getBitWidth() == Ty->getBitWidth() && "Invalid constant for type"); |
Chris Lattner | 2f7c963 | 2001-06-06 20:29:01 +0000 | [diff] [blame] | 228 | } |
| 229 | |
Owen Anderson | 23a204d | 2009-07-31 17:39:07 +0000 | [diff] [blame] | 230 | ConstantInt* ConstantInt::getTrue(LLVMContext &Context) { |
| 231 | LLVMContextImpl *pImpl = Context.pImpl; |
| 232 | sys::SmartScopedWriter<true>(pImpl->ConstantsLock); |
| 233 | if (pImpl->TheTrueVal) |
| 234 | return pImpl->TheTrueVal; |
| 235 | else |
Owen Anderson | 55f1c09 | 2009-08-13 21:58:54 +0000 | [diff] [blame] | 236 | return (pImpl->TheTrueVal = |
| 237 | ConstantInt::get(IntegerType::get(Context, 1), 1)); |
Owen Anderson | 23a204d | 2009-07-31 17:39:07 +0000 | [diff] [blame] | 238 | } |
| 239 | |
| 240 | ConstantInt* ConstantInt::getFalse(LLVMContext &Context) { |
| 241 | LLVMContextImpl *pImpl = Context.pImpl; |
| 242 | sys::SmartScopedWriter<true>(pImpl->ConstantsLock); |
| 243 | if (pImpl->TheFalseVal) |
| 244 | return pImpl->TheFalseVal; |
| 245 | else |
Owen Anderson | 55f1c09 | 2009-08-13 21:58:54 +0000 | [diff] [blame] | 246 | return (pImpl->TheFalseVal = |
| 247 | ConstantInt::get(IntegerType::get(Context, 1), 0)); |
Owen Anderson | 23a204d | 2009-07-31 17:39:07 +0000 | [diff] [blame] | 248 | } |
| 249 | |
| 250 | |
Owen Anderson | edb4a70 | 2009-07-24 23:12:02 +0000 | [diff] [blame] | 251 | // Get a ConstantInt from an APInt. Note that the value stored in the DenseMap |
| 252 | // as the key, is a DenseMapAPIntKeyInfo::KeyTy which has provided the |
| 253 | // operator== and operator!= to ensure that the DenseMap doesn't attempt to |
| 254 | // compare APInt's of different widths, which would violate an APInt class |
| 255 | // invariant which generates an assertion. |
| 256 | ConstantInt *ConstantInt::get(LLVMContext &Context, const APInt& V) { |
| 257 | // Get the corresponding integer type for the bit width of the value. |
Owen Anderson | 55f1c09 | 2009-08-13 21:58:54 +0000 | [diff] [blame] | 258 | const IntegerType *ITy = IntegerType::get(Context, V.getBitWidth()); |
Owen Anderson | edb4a70 | 2009-07-24 23:12:02 +0000 | [diff] [blame] | 259 | // get an existing value or the insertion position |
| 260 | DenseMapAPIntKeyInfo::KeyTy Key(V, ITy); |
| 261 | |
| 262 | Context.pImpl->ConstantsLock.reader_acquire(); |
| 263 | ConstantInt *&Slot = Context.pImpl->IntConstants[Key]; |
| 264 | Context.pImpl->ConstantsLock.reader_release(); |
| 265 | |
| 266 | if (!Slot) { |
| 267 | sys::SmartScopedWriter<true> Writer(Context.pImpl->ConstantsLock); |
| 268 | ConstantInt *&NewSlot = Context.pImpl->IntConstants[Key]; |
| 269 | if (!Slot) { |
| 270 | NewSlot = new ConstantInt(ITy, V); |
| 271 | } |
| 272 | |
| 273 | return NewSlot; |
| 274 | } else { |
| 275 | return Slot; |
| 276 | } |
| 277 | } |
| 278 | |
| 279 | Constant* ConstantInt::get(const Type* Ty, uint64_t V, bool isSigned) { |
| 280 | Constant *C = get(cast<IntegerType>(Ty->getScalarType()), |
| 281 | V, isSigned); |
| 282 | |
| 283 | // For vectors, broadcast the value. |
| 284 | if (const VectorType *VTy = dyn_cast<VectorType>(Ty)) |
Owen Anderson | 4aa3295 | 2009-07-28 21:19:26 +0000 | [diff] [blame] | 285 | return ConstantVector::get( |
Owen Anderson | edb4a70 | 2009-07-24 23:12:02 +0000 | [diff] [blame] | 286 | std::vector<Constant *>(VTy->getNumElements(), C)); |
| 287 | |
| 288 | return C; |
| 289 | } |
| 290 | |
| 291 | ConstantInt* ConstantInt::get(const IntegerType* Ty, uint64_t V, |
| 292 | bool isSigned) { |
| 293 | return get(Ty->getContext(), APInt(Ty->getBitWidth(), V, isSigned)); |
| 294 | } |
| 295 | |
| 296 | ConstantInt* ConstantInt::getSigned(const IntegerType* Ty, int64_t V) { |
| 297 | return get(Ty, V, true); |
| 298 | } |
| 299 | |
| 300 | Constant *ConstantInt::getSigned(const Type *Ty, int64_t V) { |
| 301 | return get(Ty, V, true); |
| 302 | } |
| 303 | |
| 304 | Constant* ConstantInt::get(const Type* Ty, const APInt& V) { |
| 305 | ConstantInt *C = get(Ty->getContext(), V); |
| 306 | assert(C->getType() == Ty->getScalarType() && |
| 307 | "ConstantInt type doesn't match the type implied by its value!"); |
| 308 | |
| 309 | // For vectors, broadcast the value. |
| 310 | if (const VectorType *VTy = dyn_cast<VectorType>(Ty)) |
Owen Anderson | 4aa3295 | 2009-07-28 21:19:26 +0000 | [diff] [blame] | 311 | return ConstantVector::get( |
Owen Anderson | edb4a70 | 2009-07-24 23:12:02 +0000 | [diff] [blame] | 312 | std::vector<Constant *>(VTy->getNumElements(), C)); |
| 313 | |
| 314 | return C; |
| 315 | } |
| 316 | |
Chris Lattner | a80bf0b | 2007-02-20 06:39:57 +0000 | [diff] [blame] | 317 | //===----------------------------------------------------------------------===// |
Chris Lattner | c6ee77d | 2007-02-20 07:17:17 +0000 | [diff] [blame] | 318 | // ConstantFP |
Chris Lattner | a80bf0b | 2007-02-20 06:39:57 +0000 | [diff] [blame] | 319 | //===----------------------------------------------------------------------===// |
| 320 | |
Rafael Espindola | f5d53d4 | 2009-07-15 17:40:42 +0000 | [diff] [blame] | 321 | static const fltSemantics *TypeToFloatSemantics(const Type *Ty) { |
Owen Anderson | 55f1c09 | 2009-08-13 21:58:54 +0000 | [diff] [blame] | 322 | if (Ty == Type::getFloatTy(Ty->getContext())) |
Rafael Espindola | f5d53d4 | 2009-07-15 17:40:42 +0000 | [diff] [blame] | 323 | return &APFloat::IEEEsingle; |
Owen Anderson | 55f1c09 | 2009-08-13 21:58:54 +0000 | [diff] [blame] | 324 | if (Ty == Type::getDoubleTy(Ty->getContext())) |
Rafael Espindola | f5d53d4 | 2009-07-15 17:40:42 +0000 | [diff] [blame] | 325 | return &APFloat::IEEEdouble; |
Owen Anderson | 55f1c09 | 2009-08-13 21:58:54 +0000 | [diff] [blame] | 326 | if (Ty == Type::getX86_FP80Ty(Ty->getContext())) |
Rafael Espindola | f5d53d4 | 2009-07-15 17:40:42 +0000 | [diff] [blame] | 327 | return &APFloat::x87DoubleExtended; |
Owen Anderson | 55f1c09 | 2009-08-13 21:58:54 +0000 | [diff] [blame] | 328 | else if (Ty == Type::getFP128Ty(Ty->getContext())) |
Rafael Espindola | f5d53d4 | 2009-07-15 17:40:42 +0000 | [diff] [blame] | 329 | return &APFloat::IEEEquad; |
| 330 | |
Owen Anderson | 55f1c09 | 2009-08-13 21:58:54 +0000 | [diff] [blame] | 331 | assert(Ty == Type::getPPC_FP128Ty(Ty->getContext()) && "Unknown FP format"); |
Rafael Espindola | f5d53d4 | 2009-07-15 17:40:42 +0000 | [diff] [blame] | 332 | return &APFloat::PPCDoubleDouble; |
| 333 | } |
| 334 | |
Owen Anderson | 69c464d | 2009-07-27 20:59:43 +0000 | [diff] [blame] | 335 | /// get() - This returns a constant fp for the specified value in the |
| 336 | /// specified type. This should only be used for simple constant values like |
| 337 | /// 2.0/1.0 etc, that are known-valid both as double and as the target format. |
| 338 | Constant* ConstantFP::get(const Type* Ty, double V) { |
| 339 | LLVMContext &Context = Ty->getContext(); |
| 340 | |
| 341 | APFloat FV(V); |
| 342 | bool ignored; |
| 343 | FV.convert(*TypeToFloatSemantics(Ty->getScalarType()), |
| 344 | APFloat::rmNearestTiesToEven, &ignored); |
| 345 | Constant *C = get(Context, FV); |
| 346 | |
| 347 | // For vectors, broadcast the value. |
| 348 | if (const VectorType *VTy = dyn_cast<VectorType>(Ty)) |
Owen Anderson | 4aa3295 | 2009-07-28 21:19:26 +0000 | [diff] [blame] | 349 | return ConstantVector::get( |
Owen Anderson | 69c464d | 2009-07-27 20:59:43 +0000 | [diff] [blame] | 350 | std::vector<Constant *>(VTy->getNumElements(), C)); |
| 351 | |
| 352 | return C; |
| 353 | } |
| 354 | |
| 355 | ConstantFP* ConstantFP::getNegativeZero(const Type* Ty) { |
| 356 | LLVMContext &Context = Ty->getContext(); |
Owen Anderson | 5a1acd9 | 2009-07-31 20:28:14 +0000 | [diff] [blame] | 357 | APFloat apf = cast <ConstantFP>(Constant::getNullValue(Ty))->getValueAPF(); |
Owen Anderson | 69c464d | 2009-07-27 20:59:43 +0000 | [diff] [blame] | 358 | apf.changeSign(); |
| 359 | return get(Context, apf); |
| 360 | } |
| 361 | |
| 362 | |
| 363 | Constant* ConstantFP::getZeroValueForNegation(const Type* Ty) { |
Owen Anderson | 69c464d | 2009-07-27 20:59:43 +0000 | [diff] [blame] | 364 | if (const VectorType *PTy = dyn_cast<VectorType>(Ty)) |
| 365 | if (PTy->getElementType()->isFloatingPoint()) { |
| 366 | std::vector<Constant*> zeros(PTy->getNumElements(), |
| 367 | getNegativeZero(PTy->getElementType())); |
Owen Anderson | 4aa3295 | 2009-07-28 21:19:26 +0000 | [diff] [blame] | 368 | return ConstantVector::get(PTy, zeros); |
Owen Anderson | 69c464d | 2009-07-27 20:59:43 +0000 | [diff] [blame] | 369 | } |
| 370 | |
| 371 | if (Ty->isFloatingPoint()) |
| 372 | return getNegativeZero(Ty); |
| 373 | |
Owen Anderson | 5a1acd9 | 2009-07-31 20:28:14 +0000 | [diff] [blame] | 374 | return Constant::getNullValue(Ty); |
Owen Anderson | 69c464d | 2009-07-27 20:59:43 +0000 | [diff] [blame] | 375 | } |
| 376 | |
| 377 | |
| 378 | // ConstantFP accessors. |
| 379 | ConstantFP* ConstantFP::get(LLVMContext &Context, const APFloat& V) { |
| 380 | DenseMapAPFloatKeyInfo::KeyTy Key(V); |
| 381 | |
| 382 | LLVMContextImpl* pImpl = Context.pImpl; |
| 383 | |
| 384 | pImpl->ConstantsLock.reader_acquire(); |
| 385 | ConstantFP *&Slot = pImpl->FPConstants[Key]; |
| 386 | pImpl->ConstantsLock.reader_release(); |
| 387 | |
| 388 | if (!Slot) { |
| 389 | sys::SmartScopedWriter<true> Writer(pImpl->ConstantsLock); |
| 390 | ConstantFP *&NewSlot = pImpl->FPConstants[Key]; |
| 391 | if (!NewSlot) { |
| 392 | const Type *Ty; |
| 393 | if (&V.getSemantics() == &APFloat::IEEEsingle) |
Owen Anderson | 55f1c09 | 2009-08-13 21:58:54 +0000 | [diff] [blame] | 394 | Ty = Type::getFloatTy(Context); |
Owen Anderson | 69c464d | 2009-07-27 20:59:43 +0000 | [diff] [blame] | 395 | else if (&V.getSemantics() == &APFloat::IEEEdouble) |
Owen Anderson | 55f1c09 | 2009-08-13 21:58:54 +0000 | [diff] [blame] | 396 | Ty = Type::getDoubleTy(Context); |
Owen Anderson | 69c464d | 2009-07-27 20:59:43 +0000 | [diff] [blame] | 397 | else if (&V.getSemantics() == &APFloat::x87DoubleExtended) |
Owen Anderson | 55f1c09 | 2009-08-13 21:58:54 +0000 | [diff] [blame] | 398 | Ty = Type::getX86_FP80Ty(Context); |
Owen Anderson | 69c464d | 2009-07-27 20:59:43 +0000 | [diff] [blame] | 399 | else if (&V.getSemantics() == &APFloat::IEEEquad) |
Owen Anderson | 55f1c09 | 2009-08-13 21:58:54 +0000 | [diff] [blame] | 400 | Ty = Type::getFP128Ty(Context); |
Owen Anderson | 69c464d | 2009-07-27 20:59:43 +0000 | [diff] [blame] | 401 | else { |
| 402 | assert(&V.getSemantics() == &APFloat::PPCDoubleDouble && |
| 403 | "Unknown FP format"); |
Owen Anderson | 55f1c09 | 2009-08-13 21:58:54 +0000 | [diff] [blame] | 404 | Ty = Type::getPPC_FP128Ty(Context); |
Owen Anderson | 69c464d | 2009-07-27 20:59:43 +0000 | [diff] [blame] | 405 | } |
| 406 | NewSlot = new ConstantFP(Ty, V); |
| 407 | } |
| 408 | |
| 409 | return NewSlot; |
| 410 | } |
| 411 | |
| 412 | return Slot; |
| 413 | } |
| 414 | |
Dale Johannesen | d246b2c | 2007-08-30 00:23:21 +0000 | [diff] [blame] | 415 | ConstantFP::ConstantFP(const Type *Ty, const APFloat& V) |
| 416 | : Constant(Ty, ConstantFPVal, 0, 0), Val(V) { |
Chris Lattner | 98bd939 | 2008-04-09 06:38:30 +0000 | [diff] [blame] | 417 | assert(&V.getSemantics() == TypeToFloatSemantics(Ty) && |
| 418 | "FP type Mismatch"); |
Chris Lattner | 2f7c963 | 2001-06-06 20:29:01 +0000 | [diff] [blame] | 419 | } |
| 420 | |
Chris Lattner | c6ee77d | 2007-02-20 07:17:17 +0000 | [diff] [blame] | 421 | bool ConstantFP::isNullValue() const { |
Dale Johannesen | a719a60 | 2007-08-24 00:56:33 +0000 | [diff] [blame] | 422 | return Val.isZero() && !Val.isNegative(); |
Chris Lattner | c6ee77d | 2007-02-20 07:17:17 +0000 | [diff] [blame] | 423 | } |
| 424 | |
Dale Johannesen | d246b2c | 2007-08-30 00:23:21 +0000 | [diff] [blame] | 425 | bool ConstantFP::isExactlyValue(const APFloat& V) const { |
| 426 | return Val.bitwiseIsEqual(V); |
Chris Lattner | c6ee77d | 2007-02-20 07:17:17 +0000 | [diff] [blame] | 427 | } |
| 428 | |
Chris Lattner | c6ee77d | 2007-02-20 07:17:17 +0000 | [diff] [blame] | 429 | //===----------------------------------------------------------------------===// |
| 430 | // ConstantXXX Classes |
| 431 | //===----------------------------------------------------------------------===// |
| 432 | |
| 433 | |
Chris Lattner | 3462ae3 | 2001-12-03 22:26:30 +0000 | [diff] [blame] | 434 | ConstantArray::ConstantArray(const ArrayType *T, |
Chris Lattner | d0df99c | 2005-01-29 00:34:39 +0000 | [diff] [blame] | 435 | const std::vector<Constant*> &V) |
Gabor Greif | f6caff66 | 2008-05-10 08:32:32 +0000 | [diff] [blame] | 436 | : Constant(T, ConstantArrayVal, |
| 437 | OperandTraits<ConstantArray>::op_end(this) - V.size(), |
| 438 | V.size()) { |
Alkis Evlogimenos | 0507ffe | 2004-09-15 02:32:15 +0000 | [diff] [blame] | 439 | assert(V.size() == T->getNumElements() && |
| 440 | "Invalid initializer vector for constant array"); |
Chris Lattner | d0df99c | 2005-01-29 00:34:39 +0000 | [diff] [blame] | 441 | Use *OL = OperandList; |
Chris Lattner | 0144fad | 2005-10-03 21:56:24 +0000 | [diff] [blame] | 442 | for (std::vector<Constant*>::const_iterator I = V.begin(), E = V.end(); |
| 443 | I != E; ++I, ++OL) { |
Chris Lattner | 20a2445 | 2005-10-07 05:23:36 +0000 | [diff] [blame] | 444 | Constant *C = *I; |
| 445 | assert((C->getType() == T->getElementType() || |
Alkis Evlogimenos | cb031d9 | 2004-09-10 04:16:59 +0000 | [diff] [blame] | 446 | (T->isAbstract() && |
Chris Lattner | 20a2445 | 2005-10-07 05:23:36 +0000 | [diff] [blame] | 447 | C->getType()->getTypeID() == T->getElementType()->getTypeID())) && |
Alkis Evlogimenos | cb031d9 | 2004-09-10 04:16:59 +0000 | [diff] [blame] | 448 | "Initializer for array element doesn't match array element type!"); |
Gabor Greif | 2d3024d | 2008-05-26 21:33:52 +0000 | [diff] [blame] | 449 | *OL = C; |
Chris Lattner | 2f7c963 | 2001-06-06 20:29:01 +0000 | [diff] [blame] | 450 | } |
| 451 | } |
| 452 | |
Owen Anderson | c2c7932 | 2009-07-28 18:32:17 +0000 | [diff] [blame] | 453 | Constant *ConstantArray::get(const ArrayType *Ty, |
| 454 | const std::vector<Constant*> &V) { |
| 455 | LLVMContextImpl *pImpl = Ty->getContext().pImpl; |
| 456 | // If this is an all-zero array, return a ConstantAggregateZero object |
| 457 | if (!V.empty()) { |
| 458 | Constant *C = V[0]; |
| 459 | if (!C->isNullValue()) { |
| 460 | // Implicitly locked. |
| 461 | return pImpl->ArrayConstants.getOrCreate(Ty, V); |
| 462 | } |
| 463 | for (unsigned i = 1, e = V.size(); i != e; ++i) |
| 464 | if (V[i] != C) { |
| 465 | // Implicitly locked. |
| 466 | return pImpl->ArrayConstants.getOrCreate(Ty, V); |
| 467 | } |
| 468 | } |
| 469 | |
Owen Anderson | b292b8c | 2009-07-30 23:03:37 +0000 | [diff] [blame] | 470 | return ConstantAggregateZero::get(Ty); |
Owen Anderson | c2c7932 | 2009-07-28 18:32:17 +0000 | [diff] [blame] | 471 | } |
| 472 | |
| 473 | |
| 474 | Constant* ConstantArray::get(const ArrayType* T, Constant* const* Vals, |
| 475 | unsigned NumVals) { |
| 476 | // FIXME: make this the primary ctor method. |
| 477 | return get(T, std::vector<Constant*>(Vals, Vals+NumVals)); |
| 478 | } |
| 479 | |
| 480 | /// ConstantArray::get(const string&) - Return an array that is initialized to |
| 481 | /// contain the specified string. If length is zero then a null terminator is |
| 482 | /// added to the specified string so that it may be used in a natural way. |
| 483 | /// Otherwise, the length parameter specifies how much of the string to use |
| 484 | /// and it won't be null terminated. |
| 485 | /// |
Owen Anderson | 55f1c09 | 2009-08-13 21:58:54 +0000 | [diff] [blame] | 486 | Constant* ConstantArray::get(LLVMContext &Context, const StringRef &Str, |
| 487 | bool AddNull) { |
Owen Anderson | c2c7932 | 2009-07-28 18:32:17 +0000 | [diff] [blame] | 488 | std::vector<Constant*> ElementVals; |
| 489 | for (unsigned i = 0; i < Str.size(); ++i) |
Owen Anderson | 55f1c09 | 2009-08-13 21:58:54 +0000 | [diff] [blame] | 490 | ElementVals.push_back(ConstantInt::get(Type::getInt8Ty(Context), Str[i])); |
Owen Anderson | c2c7932 | 2009-07-28 18:32:17 +0000 | [diff] [blame] | 491 | |
| 492 | // Add a null terminator to the string... |
| 493 | if (AddNull) { |
Owen Anderson | 55f1c09 | 2009-08-13 21:58:54 +0000 | [diff] [blame] | 494 | ElementVals.push_back(ConstantInt::get(Type::getInt8Ty(Context), 0)); |
Owen Anderson | c2c7932 | 2009-07-28 18:32:17 +0000 | [diff] [blame] | 495 | } |
| 496 | |
Owen Anderson | 55f1c09 | 2009-08-13 21:58:54 +0000 | [diff] [blame] | 497 | ArrayType *ATy = ArrayType::get(Type::getInt8Ty(Context), ElementVals.size()); |
Owen Anderson | c2c7932 | 2009-07-28 18:32:17 +0000 | [diff] [blame] | 498 | return get(ATy, ElementVals); |
| 499 | } |
| 500 | |
| 501 | |
Chris Lattner | d0df99c | 2005-01-29 00:34:39 +0000 | [diff] [blame] | 502 | |
Chris Lattner | 3462ae3 | 2001-12-03 22:26:30 +0000 | [diff] [blame] | 503 | ConstantStruct::ConstantStruct(const StructType *T, |
Chris Lattner | d0df99c | 2005-01-29 00:34:39 +0000 | [diff] [blame] | 504 | const std::vector<Constant*> &V) |
Gabor Greif | f6caff66 | 2008-05-10 08:32:32 +0000 | [diff] [blame] | 505 | : Constant(T, ConstantStructVal, |
| 506 | OperandTraits<ConstantStruct>::op_end(this) - V.size(), |
| 507 | V.size()) { |
Chris Lattner | ac6db75 | 2004-02-09 04:37:31 +0000 | [diff] [blame] | 508 | assert(V.size() == T->getNumElements() && |
Vikram S. Adve | 4e537b2 | 2002-07-14 23:13:17 +0000 | [diff] [blame] | 509 | "Invalid initializer vector for constant structure"); |
Chris Lattner | d0df99c | 2005-01-29 00:34:39 +0000 | [diff] [blame] | 510 | Use *OL = OperandList; |
Chris Lattner | 0144fad | 2005-10-03 21:56:24 +0000 | [diff] [blame] | 511 | for (std::vector<Constant*>::const_iterator I = V.begin(), E = V.end(); |
| 512 | I != E; ++I, ++OL) { |
Chris Lattner | 20a2445 | 2005-10-07 05:23:36 +0000 | [diff] [blame] | 513 | Constant *C = *I; |
| 514 | assert((C->getType() == T->getElementType(I-V.begin()) || |
Chris Lattner | 0144fad | 2005-10-03 21:56:24 +0000 | [diff] [blame] | 515 | ((T->getElementType(I-V.begin())->isAbstract() || |
Chris Lattner | 20a2445 | 2005-10-07 05:23:36 +0000 | [diff] [blame] | 516 | C->getType()->isAbstract()) && |
Chris Lattner | 0144fad | 2005-10-03 21:56:24 +0000 | [diff] [blame] | 517 | T->getElementType(I-V.begin())->getTypeID() == |
Chris Lattner | 20a2445 | 2005-10-07 05:23:36 +0000 | [diff] [blame] | 518 | C->getType()->getTypeID())) && |
Chris Lattner | 93c8f14 | 2003-06-02 17:42:47 +0000 | [diff] [blame] | 519 | "Initializer for struct element doesn't match struct element type!"); |
Gabor Greif | 2d3024d | 2008-05-26 21:33:52 +0000 | [diff] [blame] | 520 | *OL = C; |
Chris Lattner | 2f7c963 | 2001-06-06 20:29:01 +0000 | [diff] [blame] | 521 | } |
| 522 | } |
| 523 | |
Owen Anderson | 45308b5 | 2009-07-27 22:29:26 +0000 | [diff] [blame] | 524 | // ConstantStruct accessors. |
| 525 | Constant* ConstantStruct::get(const StructType* T, |
| 526 | const std::vector<Constant*>& V) { |
| 527 | LLVMContextImpl* pImpl = T->getContext().pImpl; |
| 528 | |
| 529 | // Create a ConstantAggregateZero value if all elements are zeros... |
| 530 | for (unsigned i = 0, e = V.size(); i != e; ++i) |
| 531 | if (!V[i]->isNullValue()) |
| 532 | // Implicitly locked. |
| 533 | return pImpl->StructConstants.getOrCreate(T, V); |
| 534 | |
Owen Anderson | b292b8c | 2009-07-30 23:03:37 +0000 | [diff] [blame] | 535 | return ConstantAggregateZero::get(T); |
Owen Anderson | 45308b5 | 2009-07-27 22:29:26 +0000 | [diff] [blame] | 536 | } |
| 537 | |
Owen Anderson | 03cb69f | 2009-08-05 23:16:16 +0000 | [diff] [blame] | 538 | Constant* ConstantStruct::get(LLVMContext &Context, |
| 539 | const std::vector<Constant*>& V, bool packed) { |
Owen Anderson | 45308b5 | 2009-07-27 22:29:26 +0000 | [diff] [blame] | 540 | std::vector<const Type*> StructEls; |
| 541 | StructEls.reserve(V.size()); |
| 542 | for (unsigned i = 0, e = V.size(); i != e; ++i) |
| 543 | StructEls.push_back(V[i]->getType()); |
Owen Anderson | 03cb69f | 2009-08-05 23:16:16 +0000 | [diff] [blame] | 544 | return get(StructType::get(Context, StructEls, packed), V); |
Owen Anderson | 45308b5 | 2009-07-27 22:29:26 +0000 | [diff] [blame] | 545 | } |
| 546 | |
Owen Anderson | 03cb69f | 2009-08-05 23:16:16 +0000 | [diff] [blame] | 547 | Constant* ConstantStruct::get(LLVMContext &Context, |
| 548 | Constant* const *Vals, unsigned NumVals, |
Owen Anderson | 45308b5 | 2009-07-27 22:29:26 +0000 | [diff] [blame] | 549 | bool Packed) { |
| 550 | // FIXME: make this the primary ctor method. |
Owen Anderson | 03cb69f | 2009-08-05 23:16:16 +0000 | [diff] [blame] | 551 | return get(Context, std::vector<Constant*>(Vals, Vals+NumVals), Packed); |
Owen Anderson | 45308b5 | 2009-07-27 22:29:26 +0000 | [diff] [blame] | 552 | } |
Chris Lattner | d0df99c | 2005-01-29 00:34:39 +0000 | [diff] [blame] | 553 | |
Reid Spencer | d84d35b | 2007-02-15 02:26:10 +0000 | [diff] [blame] | 554 | ConstantVector::ConstantVector(const VectorType *T, |
Chris Lattner | d0df99c | 2005-01-29 00:34:39 +0000 | [diff] [blame] | 555 | const std::vector<Constant*> &V) |
Gabor Greif | f6caff66 | 2008-05-10 08:32:32 +0000 | [diff] [blame] | 556 | : Constant(T, ConstantVectorVal, |
| 557 | OperandTraits<ConstantVector>::op_end(this) - V.size(), |
| 558 | V.size()) { |
Chris Lattner | d0df99c | 2005-01-29 00:34:39 +0000 | [diff] [blame] | 559 | Use *OL = OperandList; |
Chris Lattner | 0144fad | 2005-10-03 21:56:24 +0000 | [diff] [blame] | 560 | for (std::vector<Constant*>::const_iterator I = V.begin(), E = V.end(); |
| 561 | I != E; ++I, ++OL) { |
Chris Lattner | 20a2445 | 2005-10-07 05:23:36 +0000 | [diff] [blame] | 562 | Constant *C = *I; |
| 563 | assert((C->getType() == T->getElementType() || |
Alkis Evlogimenos | cb031d9 | 2004-09-10 04:16:59 +0000 | [diff] [blame] | 564 | (T->isAbstract() && |
Chris Lattner | 20a2445 | 2005-10-07 05:23:36 +0000 | [diff] [blame] | 565 | C->getType()->getTypeID() == T->getElementType()->getTypeID())) && |
Dan Gohman | 3097807 | 2007-05-24 14:36:04 +0000 | [diff] [blame] | 566 | "Initializer for vector element doesn't match vector element type!"); |
Gabor Greif | 2d3024d | 2008-05-26 21:33:52 +0000 | [diff] [blame] | 567 | *OL = C; |
Brian Gaeke | 0220904 | 2004-08-20 06:00:58 +0000 | [diff] [blame] | 568 | } |
| 569 | } |
| 570 | |
Owen Anderson | 4aa3295 | 2009-07-28 21:19:26 +0000 | [diff] [blame] | 571 | // ConstantVector accessors. |
| 572 | Constant* ConstantVector::get(const VectorType* T, |
| 573 | const std::vector<Constant*>& V) { |
| 574 | assert(!V.empty() && "Vectors can't be empty"); |
| 575 | LLVMContext &Context = T->getContext(); |
| 576 | LLVMContextImpl *pImpl = Context.pImpl; |
| 577 | |
| 578 | // If this is an all-undef or alll-zero vector, return a |
| 579 | // ConstantAggregateZero or UndefValue. |
| 580 | Constant *C = V[0]; |
| 581 | bool isZero = C->isNullValue(); |
| 582 | bool isUndef = isa<UndefValue>(C); |
| 583 | |
| 584 | if (isZero || isUndef) { |
| 585 | for (unsigned i = 1, e = V.size(); i != e; ++i) |
| 586 | if (V[i] != C) { |
| 587 | isZero = isUndef = false; |
| 588 | break; |
| 589 | } |
| 590 | } |
| 591 | |
| 592 | if (isZero) |
Owen Anderson | b292b8c | 2009-07-30 23:03:37 +0000 | [diff] [blame] | 593 | return ConstantAggregateZero::get(T); |
Owen Anderson | 4aa3295 | 2009-07-28 21:19:26 +0000 | [diff] [blame] | 594 | if (isUndef) |
Owen Anderson | b292b8c | 2009-07-30 23:03:37 +0000 | [diff] [blame] | 595 | return UndefValue::get(T); |
Owen Anderson | 4aa3295 | 2009-07-28 21:19:26 +0000 | [diff] [blame] | 596 | |
| 597 | // Implicitly locked. |
| 598 | return pImpl->VectorConstants.getOrCreate(T, V); |
| 599 | } |
| 600 | |
| 601 | Constant* ConstantVector::get(const std::vector<Constant*>& V) { |
| 602 | assert(!V.empty() && "Cannot infer type if V is empty"); |
| 603 | return get(VectorType::get(V.front()->getType(),V.size()), V); |
| 604 | } |
| 605 | |
| 606 | Constant* ConstantVector::get(Constant* const* Vals, unsigned NumVals) { |
| 607 | // FIXME: make this the primary ctor method. |
| 608 | return get(std::vector<Constant*>(Vals, Vals+NumVals)); |
| 609 | } |
| 610 | |
Dan Gohman | 77ad32a | 2009-08-11 20:20:39 +0000 | [diff] [blame] | 611 | Constant* ConstantExpr::getNSWAdd(Constant* C1, Constant* C2) { |
| 612 | Constant *C = getAdd(C1, C2); |
| 613 | // Set nsw attribute, assuming constant folding didn't eliminate the |
| 614 | // Add. |
| 615 | if (AddOperator *Add = dyn_cast<AddOperator>(C)) |
| 616 | Add->setHasNoSignedOverflow(true); |
| 617 | return C; |
| 618 | } |
| 619 | |
Dan Gohman | 58f4d89 | 2009-08-11 17:05:24 +0000 | [diff] [blame] | 620 | Constant* ConstantExpr::getExactSDiv(Constant* C1, Constant* C2) { |
| 621 | Constant *C = getSDiv(C1, C2); |
Dan Gohman | 221a2c7 | 2009-08-11 19:56:00 +0000 | [diff] [blame] | 622 | // Set exact attribute, assuming constant folding didn't eliminate the |
| 623 | // SDiv. |
| 624 | if (SDivOperator *SDiv = dyn_cast<SDivOperator>(C)) |
| 625 | SDiv->setIsExact(true); |
Dan Gohman | 58f4d89 | 2009-08-11 17:05:24 +0000 | [diff] [blame] | 626 | return C; |
| 627 | } |
| 628 | |
Reid Spencer | 6c38f0b | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 629 | // Utility function for determining if a ConstantExpr is a CastOp or not. This |
| 630 | // can't be inline because we don't want to #include Instruction.h into |
| 631 | // Constant.h |
| 632 | bool ConstantExpr::isCast() const { |
| 633 | return Instruction::isCast(getOpcode()); |
| 634 | } |
| 635 | |
Reid Spencer | ee3c991 | 2006-12-04 05:19:50 +0000 | [diff] [blame] | 636 | bool ConstantExpr::isCompare() const { |
Nick Lewycky | a21d3da | 2009-07-08 03:04:38 +0000 | [diff] [blame] | 637 | return getOpcode() == Instruction::ICmp || getOpcode() == Instruction::FCmp; |
Reid Spencer | ee3c991 | 2006-12-04 05:19:50 +0000 | [diff] [blame] | 638 | } |
| 639 | |
Dan Gohman | 1ecaf45 | 2008-05-31 00:58:22 +0000 | [diff] [blame] | 640 | bool ConstantExpr::hasIndices() const { |
| 641 | return getOpcode() == Instruction::ExtractValue || |
| 642 | getOpcode() == Instruction::InsertValue; |
| 643 | } |
| 644 | |
| 645 | const SmallVector<unsigned, 4> &ConstantExpr::getIndices() const { |
| 646 | if (const ExtractValueConstantExpr *EVCE = |
| 647 | dyn_cast<ExtractValueConstantExpr>(this)) |
| 648 | return EVCE->Indices; |
Dan Gohman | a469bdb | 2008-06-23 16:39:44 +0000 | [diff] [blame] | 649 | |
| 650 | return cast<InsertValueConstantExpr>(this)->Indices; |
Dan Gohman | 1ecaf45 | 2008-05-31 00:58:22 +0000 | [diff] [blame] | 651 | } |
| 652 | |
Reid Spencer | 10fbf0e | 2006-12-03 05:48:19 +0000 | [diff] [blame] | 653 | unsigned ConstantExpr::getPredicate() const { |
Nate Begeman | d219570 | 2008-05-12 19:01:56 +0000 | [diff] [blame] | 654 | assert(getOpcode() == Instruction::FCmp || |
Nick Lewycky | a21d3da | 2009-07-08 03:04:38 +0000 | [diff] [blame] | 655 | getOpcode() == Instruction::ICmp); |
Chris Lattner | ef65009 | 2007-10-18 16:26:24 +0000 | [diff] [blame] | 656 | return ((const CompareConstantExpr*)this)->predicate; |
Reid Spencer | 10fbf0e | 2006-12-03 05:48:19 +0000 | [diff] [blame] | 657 | } |
Chris Lattner | 60e0dd7 | 2001-10-03 06:12:09 +0000 | [diff] [blame] | 658 | |
Chris Lattner | 7c1018a | 2006-07-14 19:37:40 +0000 | [diff] [blame] | 659 | /// getWithOperandReplaced - Return a constant expression identical to this |
| 660 | /// one, but with the specified operand set to the specified value. |
Reid Spencer | 6c38f0b | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 661 | Constant * |
| 662 | ConstantExpr::getWithOperandReplaced(unsigned OpNo, Constant *Op) const { |
Chris Lattner | 7c1018a | 2006-07-14 19:37:40 +0000 | [diff] [blame] | 663 | assert(OpNo < getNumOperands() && "Operand num is out of range!"); |
| 664 | assert(Op->getType() == getOperand(OpNo)->getType() && |
| 665 | "Replacing operand with value of different type!"); |
Chris Lattner | 22781634 | 2006-07-14 22:20:01 +0000 | [diff] [blame] | 666 | if (getOperand(OpNo) == Op) |
| 667 | return const_cast<ConstantExpr*>(this); |
Chris Lattner | 7c1018a | 2006-07-14 19:37:40 +0000 | [diff] [blame] | 668 | |
Chris Lattner | 22781634 | 2006-07-14 22:20:01 +0000 | [diff] [blame] | 669 | Constant *Op0, *Op1, *Op2; |
Chris Lattner | 7c1018a | 2006-07-14 19:37:40 +0000 | [diff] [blame] | 670 | switch (getOpcode()) { |
Reid Spencer | 6c38f0b | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 671 | case Instruction::Trunc: |
| 672 | case Instruction::ZExt: |
| 673 | case Instruction::SExt: |
| 674 | case Instruction::FPTrunc: |
| 675 | case Instruction::FPExt: |
| 676 | case Instruction::UIToFP: |
| 677 | case Instruction::SIToFP: |
| 678 | case Instruction::FPToUI: |
| 679 | case Instruction::FPToSI: |
| 680 | case Instruction::PtrToInt: |
| 681 | case Instruction::IntToPtr: |
| 682 | case Instruction::BitCast: |
| 683 | return ConstantExpr::getCast(getOpcode(), Op, getType()); |
Chris Lattner | 22781634 | 2006-07-14 22:20:01 +0000 | [diff] [blame] | 684 | case Instruction::Select: |
| 685 | Op0 = (OpNo == 0) ? Op : getOperand(0); |
| 686 | Op1 = (OpNo == 1) ? Op : getOperand(1); |
| 687 | Op2 = (OpNo == 2) ? Op : getOperand(2); |
| 688 | return ConstantExpr::getSelect(Op0, Op1, Op2); |
| 689 | case Instruction::InsertElement: |
| 690 | Op0 = (OpNo == 0) ? Op : getOperand(0); |
| 691 | Op1 = (OpNo == 1) ? Op : getOperand(1); |
| 692 | Op2 = (OpNo == 2) ? Op : getOperand(2); |
| 693 | return ConstantExpr::getInsertElement(Op0, Op1, Op2); |
| 694 | case Instruction::ExtractElement: |
| 695 | Op0 = (OpNo == 0) ? Op : getOperand(0); |
| 696 | Op1 = (OpNo == 1) ? Op : getOperand(1); |
| 697 | return ConstantExpr::getExtractElement(Op0, Op1); |
| 698 | case Instruction::ShuffleVector: |
| 699 | Op0 = (OpNo == 0) ? Op : getOperand(0); |
| 700 | Op1 = (OpNo == 1) ? Op : getOperand(1); |
| 701 | Op2 = (OpNo == 2) ? Op : getOperand(2); |
| 702 | return ConstantExpr::getShuffleVector(Op0, Op1, Op2); |
Chris Lattner | 7c1018a | 2006-07-14 19:37:40 +0000 | [diff] [blame] | 703 | case Instruction::GetElementPtr: { |
Chris Lattner | b5d7030 | 2007-02-19 20:01:23 +0000 | [diff] [blame] | 704 | SmallVector<Constant*, 8> Ops; |
Dan Gohman | 12fce77 | 2008-05-15 19:50:34 +0000 | [diff] [blame] | 705 | Ops.resize(getNumOperands()-1); |
Chris Lattner | 7c1018a | 2006-07-14 19:37:40 +0000 | [diff] [blame] | 706 | for (unsigned i = 1, e = getNumOperands(); i != e; ++i) |
Dan Gohman | 12fce77 | 2008-05-15 19:50:34 +0000 | [diff] [blame] | 707 | Ops[i-1] = getOperand(i); |
Chris Lattner | 7c1018a | 2006-07-14 19:37:40 +0000 | [diff] [blame] | 708 | if (OpNo == 0) |
Chris Lattner | b5d7030 | 2007-02-19 20:01:23 +0000 | [diff] [blame] | 709 | return ConstantExpr::getGetElementPtr(Op, &Ops[0], Ops.size()); |
Chris Lattner | 7c1018a | 2006-07-14 19:37:40 +0000 | [diff] [blame] | 710 | Ops[OpNo-1] = Op; |
Chris Lattner | b5d7030 | 2007-02-19 20:01:23 +0000 | [diff] [blame] | 711 | return ConstantExpr::getGetElementPtr(getOperand(0), &Ops[0], Ops.size()); |
Chris Lattner | 7c1018a | 2006-07-14 19:37:40 +0000 | [diff] [blame] | 712 | } |
Chris Lattner | 7c1018a | 2006-07-14 19:37:40 +0000 | [diff] [blame] | 713 | default: |
| 714 | assert(getNumOperands() == 2 && "Must be binary operator?"); |
Chris Lattner | 22781634 | 2006-07-14 22:20:01 +0000 | [diff] [blame] | 715 | Op0 = (OpNo == 0) ? Op : getOperand(0); |
| 716 | Op1 = (OpNo == 1) ? Op : getOperand(1); |
| 717 | return ConstantExpr::get(getOpcode(), Op0, Op1); |
| 718 | } |
| 719 | } |
| 720 | |
| 721 | /// getWithOperands - This returns the current constant expression with the |
| 722 | /// operands replaced with the specified values. The specified operands must |
| 723 | /// match count and type with the existing ones. |
| 724 | Constant *ConstantExpr:: |
Chris Lattner | b078e28 | 2008-08-20 22:27:40 +0000 | [diff] [blame] | 725 | getWithOperands(Constant* const *Ops, unsigned NumOps) const { |
| 726 | assert(NumOps == getNumOperands() && "Operand count mismatch!"); |
Chris Lattner | 22781634 | 2006-07-14 22:20:01 +0000 | [diff] [blame] | 727 | bool AnyChange = false; |
Chris Lattner | b078e28 | 2008-08-20 22:27:40 +0000 | [diff] [blame] | 728 | for (unsigned i = 0; i != NumOps; ++i) { |
Chris Lattner | 22781634 | 2006-07-14 22:20:01 +0000 | [diff] [blame] | 729 | assert(Ops[i]->getType() == getOperand(i)->getType() && |
| 730 | "Operand type mismatch!"); |
| 731 | AnyChange |= Ops[i] != getOperand(i); |
| 732 | } |
| 733 | if (!AnyChange) // No operands changed, return self. |
| 734 | return const_cast<ConstantExpr*>(this); |
| 735 | |
| 736 | switch (getOpcode()) { |
Reid Spencer | 6c38f0b | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 737 | case Instruction::Trunc: |
| 738 | case Instruction::ZExt: |
| 739 | case Instruction::SExt: |
| 740 | case Instruction::FPTrunc: |
| 741 | case Instruction::FPExt: |
| 742 | case Instruction::UIToFP: |
| 743 | case Instruction::SIToFP: |
| 744 | case Instruction::FPToUI: |
| 745 | case Instruction::FPToSI: |
| 746 | case Instruction::PtrToInt: |
| 747 | case Instruction::IntToPtr: |
| 748 | case Instruction::BitCast: |
| 749 | return ConstantExpr::getCast(getOpcode(), Ops[0], getType()); |
Chris Lattner | 22781634 | 2006-07-14 22:20:01 +0000 | [diff] [blame] | 750 | case Instruction::Select: |
| 751 | return ConstantExpr::getSelect(Ops[0], Ops[1], Ops[2]); |
| 752 | case Instruction::InsertElement: |
| 753 | return ConstantExpr::getInsertElement(Ops[0], Ops[1], Ops[2]); |
| 754 | case Instruction::ExtractElement: |
| 755 | return ConstantExpr::getExtractElement(Ops[0], Ops[1]); |
| 756 | case Instruction::ShuffleVector: |
| 757 | return ConstantExpr::getShuffleVector(Ops[0], Ops[1], Ops[2]); |
Chris Lattner | b5d7030 | 2007-02-19 20:01:23 +0000 | [diff] [blame] | 758 | case Instruction::GetElementPtr: |
Chris Lattner | b078e28 | 2008-08-20 22:27:40 +0000 | [diff] [blame] | 759 | return ConstantExpr::getGetElementPtr(Ops[0], &Ops[1], NumOps-1); |
Reid Spencer | 266e42b | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 760 | case Instruction::ICmp: |
| 761 | case Instruction::FCmp: |
| 762 | return ConstantExpr::getCompare(getPredicate(), Ops[0], Ops[1]); |
Chris Lattner | 22781634 | 2006-07-14 22:20:01 +0000 | [diff] [blame] | 763 | default: |
| 764 | assert(getNumOperands() == 2 && "Must be binary operator?"); |
| 765 | return ConstantExpr::get(getOpcode(), Ops[0], Ops[1]); |
Chris Lattner | 7c1018a | 2006-07-14 19:37:40 +0000 | [diff] [blame] | 766 | } |
| 767 | } |
| 768 | |
Chris Lattner | 2f7c963 | 2001-06-06 20:29:01 +0000 | [diff] [blame] | 769 | |
| 770 | //===----------------------------------------------------------------------===// |
Chris Lattner | 2f7c963 | 2001-06-06 20:29:01 +0000 | [diff] [blame] | 771 | // isValueValidForType implementations |
| 772 | |
Reid Spencer | e733472 | 2006-12-19 01:28:19 +0000 | [diff] [blame] | 773 | bool ConstantInt::isValueValidForType(const Type *Ty, uint64_t Val) { |
Reid Spencer | 7a9c62b | 2007-01-12 07:05:14 +0000 | [diff] [blame] | 774 | unsigned NumBits = cast<IntegerType>(Ty)->getBitWidth(); // assert okay |
Owen Anderson | 55f1c09 | 2009-08-13 21:58:54 +0000 | [diff] [blame] | 775 | if (Ty == Type::getInt1Ty(Ty->getContext())) |
Reid Spencer | 7a9c62b | 2007-01-12 07:05:14 +0000 | [diff] [blame] | 776 | return Val == 0 || Val == 1; |
Reid Spencer | d7a00d7 | 2007-02-05 23:47:56 +0000 | [diff] [blame] | 777 | if (NumBits >= 64) |
Reid Spencer | 7a9c62b | 2007-01-12 07:05:14 +0000 | [diff] [blame] | 778 | return true; // always true, has to fit in largest type |
| 779 | uint64_t Max = (1ll << NumBits) - 1; |
| 780 | return Val <= Max; |
Reid Spencer | e733472 | 2006-12-19 01:28:19 +0000 | [diff] [blame] | 781 | } |
| 782 | |
Reid Spencer | e0fc4df | 2006-10-20 07:07:24 +0000 | [diff] [blame] | 783 | bool ConstantInt::isValueValidForType(const Type *Ty, int64_t Val) { |
Reid Spencer | 7a9c62b | 2007-01-12 07:05:14 +0000 | [diff] [blame] | 784 | unsigned NumBits = cast<IntegerType>(Ty)->getBitWidth(); // assert okay |
Owen Anderson | 55f1c09 | 2009-08-13 21:58:54 +0000 | [diff] [blame] | 785 | if (Ty == Type::getInt1Ty(Ty->getContext())) |
Reid Spencer | a94d394 | 2007-01-19 21:13:56 +0000 | [diff] [blame] | 786 | return Val == 0 || Val == 1 || Val == -1; |
Reid Spencer | d7a00d7 | 2007-02-05 23:47:56 +0000 | [diff] [blame] | 787 | if (NumBits >= 64) |
Reid Spencer | 7a9c62b | 2007-01-12 07:05:14 +0000 | [diff] [blame] | 788 | return true; // always true, has to fit in largest type |
| 789 | int64_t Min = -(1ll << (NumBits-1)); |
| 790 | int64_t Max = (1ll << (NumBits-1)) - 1; |
| 791 | return (Val >= Min && Val <= Max); |
Chris Lattner | 2f7c963 | 2001-06-06 20:29:01 +0000 | [diff] [blame] | 792 | } |
| 793 | |
Dale Johannesen | d246b2c | 2007-08-30 00:23:21 +0000 | [diff] [blame] | 794 | bool ConstantFP::isValueValidForType(const Type *Ty, const APFloat& Val) { |
| 795 | // convert modifies in place, so make a copy. |
| 796 | APFloat Val2 = APFloat(Val); |
Dale Johannesen | 4f0bd68 | 2008-10-09 23:00:39 +0000 | [diff] [blame] | 797 | bool losesInfo; |
Chris Lattner | 6b72759 | 2004-06-17 18:19:28 +0000 | [diff] [blame] | 798 | switch (Ty->getTypeID()) { |
Chris Lattner | 2f7c963 | 2001-06-06 20:29:01 +0000 | [diff] [blame] | 799 | default: |
| 800 | return false; // These can't be represented as floating point! |
| 801 | |
Dale Johannesen | d246b2c | 2007-08-30 00:23:21 +0000 | [diff] [blame] | 802 | // FIXME rounding mode needs to be more flexible |
Dale Johannesen | 4f0bd68 | 2008-10-09 23:00:39 +0000 | [diff] [blame] | 803 | case Type::FloatTyID: { |
| 804 | if (&Val2.getSemantics() == &APFloat::IEEEsingle) |
| 805 | return true; |
| 806 | Val2.convert(APFloat::IEEEsingle, APFloat::rmNearestTiesToEven, &losesInfo); |
| 807 | return !losesInfo; |
| 808 | } |
| 809 | case Type::DoubleTyID: { |
| 810 | if (&Val2.getSemantics() == &APFloat::IEEEsingle || |
| 811 | &Val2.getSemantics() == &APFloat::IEEEdouble) |
| 812 | return true; |
| 813 | Val2.convert(APFloat::IEEEdouble, APFloat::rmNearestTiesToEven, &losesInfo); |
| 814 | return !losesInfo; |
| 815 | } |
Dale Johannesen | bdad809 | 2007-08-09 22:51:36 +0000 | [diff] [blame] | 816 | case Type::X86_FP80TyID: |
Dale Johannesen | 028084e | 2007-09-12 03:30:33 +0000 | [diff] [blame] | 817 | return &Val2.getSemantics() == &APFloat::IEEEsingle || |
| 818 | &Val2.getSemantics() == &APFloat::IEEEdouble || |
| 819 | &Val2.getSemantics() == &APFloat::x87DoubleExtended; |
Dale Johannesen | bdad809 | 2007-08-09 22:51:36 +0000 | [diff] [blame] | 820 | case Type::FP128TyID: |
Dale Johannesen | 028084e | 2007-09-12 03:30:33 +0000 | [diff] [blame] | 821 | return &Val2.getSemantics() == &APFloat::IEEEsingle || |
| 822 | &Val2.getSemantics() == &APFloat::IEEEdouble || |
| 823 | &Val2.getSemantics() == &APFloat::IEEEquad; |
Dale Johannesen | 007aa37 | 2007-10-11 18:07:22 +0000 | [diff] [blame] | 824 | case Type::PPC_FP128TyID: |
| 825 | return &Val2.getSemantics() == &APFloat::IEEEsingle || |
| 826 | &Val2.getSemantics() == &APFloat::IEEEdouble || |
| 827 | &Val2.getSemantics() == &APFloat::PPCDoubleDouble; |
Chris Lattner | 2f7c963 | 2001-06-06 20:29:01 +0000 | [diff] [blame] | 828 | } |
Chris Lattner | aa237256 | 2006-05-24 17:04:05 +0000 | [diff] [blame] | 829 | } |
Chris Lattner | 9655e54 | 2001-07-20 19:16:02 +0000 | [diff] [blame] | 830 | |
Chris Lattner | 49d855c | 2001-09-07 16:46:31 +0000 | [diff] [blame] | 831 | //===----------------------------------------------------------------------===// |
Chris Lattner | 49d855c | 2001-09-07 16:46:31 +0000 | [diff] [blame] | 832 | // Factory Function Implementation |
| 833 | |
Owen Anderson | b292b8c | 2009-07-30 23:03:37 +0000 | [diff] [blame] | 834 | static char getValType(ConstantAggregateZero *CPZ) { return 0; } |
| 835 | |
| 836 | ConstantAggregateZero* ConstantAggregateZero::get(const Type* Ty) { |
| 837 | assert((isa<StructType>(Ty) || isa<ArrayType>(Ty) || isa<VectorType>(Ty)) && |
| 838 | "Cannot create an aggregate zero of non-aggregate type!"); |
| 839 | |
| 840 | LLVMContextImpl *pImpl = Ty->getContext().pImpl; |
| 841 | // Implicitly locked. |
| 842 | return pImpl->AggZeroConstants.getOrCreate(Ty, 0); |
| 843 | } |
| 844 | |
Dan Gohman | 92b551b | 2009-03-03 02:55:14 +0000 | [diff] [blame] | 845 | /// destroyConstant - Remove the constant from the constant table... |
| 846 | /// |
Owen Anderson | 0d2de8c | 2009-06-20 00:24:58 +0000 | [diff] [blame] | 847 | void ConstantAggregateZero::destroyConstant() { |
Owen Anderson | 6179404 | 2009-06-17 20:10:08 +0000 | [diff] [blame] | 848 | // Implicitly locked. |
Owen Anderson | b292b8c | 2009-07-30 23:03:37 +0000 | [diff] [blame] | 849 | getType()->getContext().pImpl->AggZeroConstants.remove(this); |
Chris Lattner | 9fba3da | 2004-02-15 05:53:04 +0000 | [diff] [blame] | 850 | destroyConstantImpl(); |
| 851 | } |
| 852 | |
Dan Gohman | 92b551b | 2009-03-03 02:55:14 +0000 | [diff] [blame] | 853 | /// destroyConstant - Remove the constant from the constant table... |
| 854 | /// |
Owen Anderson | 0d2de8c | 2009-06-20 00:24:58 +0000 | [diff] [blame] | 855 | void ConstantArray::destroyConstant() { |
Owen Anderson | 59ba814 | 2009-06-19 18:34:09 +0000 | [diff] [blame] | 856 | // Implicitly locked. |
Owen Anderson | c2c7932 | 2009-07-28 18:32:17 +0000 | [diff] [blame] | 857 | getType()->getContext().pImpl->ArrayConstants.remove(this); |
Chris Lattner | 98fa07b | 2003-05-23 20:03:32 +0000 | [diff] [blame] | 858 | destroyConstantImpl(); |
| 859 | } |
| 860 | |
Reid Spencer | 2546b76 | 2007-01-26 07:37:34 +0000 | [diff] [blame] | 861 | /// isString - This method returns true if the array is an array of i8, and |
| 862 | /// if the elements of the array are all ConstantInt's. |
Chris Lattner | e8dfcca | 2004-01-14 17:06:38 +0000 | [diff] [blame] | 863 | bool ConstantArray::isString() const { |
Reid Spencer | 2546b76 | 2007-01-26 07:37:34 +0000 | [diff] [blame] | 864 | // Check the element type for i8... |
Owen Anderson | 55f1c09 | 2009-08-13 21:58:54 +0000 | [diff] [blame] | 865 | if (getType()->getElementType() != Type::getInt8Ty(getContext())) |
Chris Lattner | e8dfcca | 2004-01-14 17:06:38 +0000 | [diff] [blame] | 866 | return false; |
| 867 | // Check the elements to make sure they are all integers, not constant |
| 868 | // expressions. |
| 869 | for (unsigned i = 0, e = getNumOperands(); i != e; ++i) |
| 870 | if (!isa<ConstantInt>(getOperand(i))) |
| 871 | return false; |
| 872 | return true; |
| 873 | } |
| 874 | |
Evan Cheng | 3763c5b | 2006-10-26 19:15:05 +0000 | [diff] [blame] | 875 | /// isCString - This method returns true if the array is a string (see |
Dan Gohman | 92b551b | 2009-03-03 02:55:14 +0000 | [diff] [blame] | 876 | /// isString) and it ends in a null byte \\0 and does not contains any other |
Evan Cheng | 3763c5b | 2006-10-26 19:15:05 +0000 | [diff] [blame] | 877 | /// null bytes except its terminator. |
Owen Anderson | e4dcecd | 2009-07-13 21:27:19 +0000 | [diff] [blame] | 878 | bool ConstantArray::isCString() const { |
Reid Spencer | 2546b76 | 2007-01-26 07:37:34 +0000 | [diff] [blame] | 879 | // Check the element type for i8... |
Owen Anderson | 55f1c09 | 2009-08-13 21:58:54 +0000 | [diff] [blame] | 880 | if (getType()->getElementType() != Type::getInt8Ty(getContext())) |
Evan Cheng | e974da6 | 2006-10-26 21:48:03 +0000 | [diff] [blame] | 881 | return false; |
Owen Anderson | e4dcecd | 2009-07-13 21:27:19 +0000 | [diff] [blame] | 882 | |
Evan Cheng | e974da6 | 2006-10-26 21:48:03 +0000 | [diff] [blame] | 883 | // Last element must be a null. |
Owen Anderson | e4dcecd | 2009-07-13 21:27:19 +0000 | [diff] [blame] | 884 | if (!getOperand(getNumOperands()-1)->isNullValue()) |
Evan Cheng | e974da6 | 2006-10-26 21:48:03 +0000 | [diff] [blame] | 885 | return false; |
| 886 | // Other elements must be non-null integers. |
| 887 | for (unsigned i = 0, e = getNumOperands()-1; i != e; ++i) { |
| 888 | if (!isa<ConstantInt>(getOperand(i))) |
Evan Cheng | 3763c5b | 2006-10-26 19:15:05 +0000 | [diff] [blame] | 889 | return false; |
Owen Anderson | e4dcecd | 2009-07-13 21:27:19 +0000 | [diff] [blame] | 890 | if (getOperand(i)->isNullValue()) |
Evan Cheng | e974da6 | 2006-10-26 21:48:03 +0000 | [diff] [blame] | 891 | return false; |
| 892 | } |
Evan Cheng | 3763c5b | 2006-10-26 19:15:05 +0000 | [diff] [blame] | 893 | return true; |
| 894 | } |
| 895 | |
| 896 | |
Dan Gohman | 92b551b | 2009-03-03 02:55:14 +0000 | [diff] [blame] | 897 | /// getAsString - If the sub-element type of this array is i8 |
| 898 | /// then this method converts the array to an std::string and returns it. |
| 899 | /// Otherwise, it asserts out. |
| 900 | /// |
Chris Lattner | 81fabb0 | 2002-08-26 17:53:56 +0000 | [diff] [blame] | 901 | std::string ConstantArray::getAsString() const { |
Chris Lattner | e8dfcca | 2004-01-14 17:06:38 +0000 | [diff] [blame] | 902 | assert(isString() && "Not a string!"); |
Chris Lattner | 81fabb0 | 2002-08-26 17:53:56 +0000 | [diff] [blame] | 903 | std::string Result; |
Owen Anderson | 79c69bc | 2008-06-24 21:58:29 +0000 | [diff] [blame] | 904 | Result.reserve(getNumOperands()); |
Chris Lattner | 6077c31 | 2003-07-23 15:22:26 +0000 | [diff] [blame] | 905 | for (unsigned i = 0, e = getNumOperands(); i != e; ++i) |
Owen Anderson | ee9c30d | 2008-06-25 01:05:05 +0000 | [diff] [blame] | 906 | Result.push_back((char)cast<ConstantInt>(getOperand(i))->getZExtValue()); |
Chris Lattner | 81fabb0 | 2002-08-26 17:53:56 +0000 | [diff] [blame] | 907 | return Result; |
| 908 | } |
| 909 | |
| 910 | |
Chris Lattner | 3462ae3 | 2001-12-03 22:26:30 +0000 | [diff] [blame] | 911 | //---- ConstantStruct::get() implementation... |
Chris Lattner | 49d855c | 2001-09-07 16:46:31 +0000 | [diff] [blame] | 912 | // |
Chris Lattner | b50d135 | 2003-10-05 00:17:43 +0000 | [diff] [blame] | 913 | |
Chris Lattner | 189d19f | 2003-11-21 20:23:48 +0000 | [diff] [blame] | 914 | namespace llvm { |
Misha Brukman | b1c9317 | 2005-04-21 23:48:37 +0000 | [diff] [blame] | 915 | |
Chris Lattner | 49d855c | 2001-09-07 16:46:31 +0000 | [diff] [blame] | 916 | } |
Chris Lattner | 883ad0b | 2001-10-03 15:39:36 +0000 | [diff] [blame] | 917 | |
Chris Lattner | d7a7330 | 2001-10-13 06:57:33 +0000 | [diff] [blame] | 918 | // destroyConstant - Remove the constant from the constant table... |
Chris Lattner | 883ad0b | 2001-10-03 15:39:36 +0000 | [diff] [blame] | 919 | // |
Owen Anderson | 0d2de8c | 2009-06-20 00:24:58 +0000 | [diff] [blame] | 920 | void ConstantStruct::destroyConstant() { |
Owen Anderson | 59ba814 | 2009-06-19 18:34:09 +0000 | [diff] [blame] | 921 | // Implicitly locked. |
Owen Anderson | 45308b5 | 2009-07-27 22:29:26 +0000 | [diff] [blame] | 922 | getType()->getContext().pImpl->StructConstants.remove(this); |
Chris Lattner | d7a7330 | 2001-10-13 06:57:33 +0000 | [diff] [blame] | 923 | destroyConstantImpl(); |
| 924 | } |
Chris Lattner | 883ad0b | 2001-10-03 15:39:36 +0000 | [diff] [blame] | 925 | |
Brian Gaeke | 0220904 | 2004-08-20 06:00:58 +0000 | [diff] [blame] | 926 | // destroyConstant - Remove the constant from the constant table... |
| 927 | // |
Owen Anderson | 0d2de8c | 2009-06-20 00:24:58 +0000 | [diff] [blame] | 928 | void ConstantVector::destroyConstant() { |
Owen Anderson | 59ba814 | 2009-06-19 18:34:09 +0000 | [diff] [blame] | 929 | // Implicitly locked. |
Owen Anderson | 4aa3295 | 2009-07-28 21:19:26 +0000 | [diff] [blame] | 930 | getType()->getContext().pImpl->VectorConstants.remove(this); |
Brian Gaeke | 0220904 | 2004-08-20 06:00:58 +0000 | [diff] [blame] | 931 | destroyConstantImpl(); |
| 932 | } |
| 933 | |
Dan Gohman | 3097807 | 2007-05-24 14:36:04 +0000 | [diff] [blame] | 934 | /// This function will return true iff every element in this vector constant |
Jim Laskey | f047882 | 2007-01-12 22:39:14 +0000 | [diff] [blame] | 935 | /// is set to all ones. |
| 936 | /// @returns true iff this constant's emements are all set to all ones. |
| 937 | /// @brief Determine if the value is all ones. |
Reid Spencer | d84d35b | 2007-02-15 02:26:10 +0000 | [diff] [blame] | 938 | bool ConstantVector::isAllOnesValue() const { |
Jim Laskey | f047882 | 2007-01-12 22:39:14 +0000 | [diff] [blame] | 939 | // Check out first element. |
| 940 | const Constant *Elt = getOperand(0); |
| 941 | const ConstantInt *CI = dyn_cast<ConstantInt>(Elt); |
| 942 | if (!CI || !CI->isAllOnesValue()) return false; |
| 943 | // Then make sure all remaining elements point to the same value. |
| 944 | for (unsigned I = 1, E = getNumOperands(); I < E; ++I) { |
| 945 | if (getOperand(I) != Elt) return false; |
| 946 | } |
| 947 | return true; |
| 948 | } |
| 949 | |
Dan Gohman | 0715920 | 2007-10-17 17:51:30 +0000 | [diff] [blame] | 950 | /// getSplatValue - If this is a splat constant, where all of the |
| 951 | /// elements have the same value, return that value. Otherwise return null. |
| 952 | Constant *ConstantVector::getSplatValue() { |
| 953 | // Check out first element. |
| 954 | Constant *Elt = getOperand(0); |
| 955 | // Then make sure all remaining elements point to the same value. |
| 956 | for (unsigned I = 1, E = getNumOperands(); I < E; ++I) |
| 957 | if (getOperand(I) != Elt) return 0; |
| 958 | return Elt; |
| 959 | } |
| 960 | |
Chris Lattner | 3462ae3 | 2001-12-03 22:26:30 +0000 | [diff] [blame] | 961 | //---- ConstantPointerNull::get() implementation... |
Chris Lattner | d7a7330 | 2001-10-13 06:57:33 +0000 | [diff] [blame] | 962 | // |
Chris Lattner | 98fa07b | 2003-05-23 20:03:32 +0000 | [diff] [blame] | 963 | |
Chris Lattner | 3e650af | 2004-08-04 04:48:01 +0000 | [diff] [blame] | 964 | static char getValType(ConstantPointerNull *) { |
| 965 | return 0; |
| 966 | } |
| 967 | |
| 968 | |
Owen Anderson | 0d2de8c | 2009-06-20 00:24:58 +0000 | [diff] [blame] | 969 | ConstantPointerNull *ConstantPointerNull::get(const PointerType *Ty) { |
Owen Anderson | 6179404 | 2009-06-17 20:10:08 +0000 | [diff] [blame] | 970 | // Implicitly locked. |
Owen Anderson | c8c3026 | 2009-07-31 22:45:43 +0000 | [diff] [blame] | 971 | return Ty->getContext().pImpl->NullPtrConstants.getOrCreate(Ty, 0); |
Chris Lattner | 883ad0b | 2001-10-03 15:39:36 +0000 | [diff] [blame] | 972 | } |
| 973 | |
Chris Lattner | 0c6e0b9 | 2002-08-18 00:40:04 +0000 | [diff] [blame] | 974 | // destroyConstant - Remove the constant from the constant table... |
| 975 | // |
Owen Anderson | 0d2de8c | 2009-06-20 00:24:58 +0000 | [diff] [blame] | 976 | void ConstantPointerNull::destroyConstant() { |
Owen Anderson | 59ba814 | 2009-06-19 18:34:09 +0000 | [diff] [blame] | 977 | // Implicitly locked. |
Owen Anderson | c8c3026 | 2009-07-31 22:45:43 +0000 | [diff] [blame] | 978 | getType()->getContext().pImpl->NullPtrConstants.remove(this); |
Chris Lattner | 0c6e0b9 | 2002-08-18 00:40:04 +0000 | [diff] [blame] | 979 | destroyConstantImpl(); |
| 980 | } |
| 981 | |
| 982 | |
Chris Lattner | d5f67d8 | 2004-10-16 18:07:16 +0000 | [diff] [blame] | 983 | //---- UndefValue::get() implementation... |
| 984 | // |
| 985 | |
Chris Lattner | d5f67d8 | 2004-10-16 18:07:16 +0000 | [diff] [blame] | 986 | static char getValType(UndefValue *) { |
| 987 | return 0; |
| 988 | } |
| 989 | |
Owen Anderson | 0d2de8c | 2009-06-20 00:24:58 +0000 | [diff] [blame] | 990 | UndefValue *UndefValue::get(const Type *Ty) { |
| 991 | // Implicitly locked. |
Owen Anderson | c8c3026 | 2009-07-31 22:45:43 +0000 | [diff] [blame] | 992 | return Ty->getContext().pImpl->UndefValueConstants.getOrCreate(Ty, 0); |
Chris Lattner | d5f67d8 | 2004-10-16 18:07:16 +0000 | [diff] [blame] | 993 | } |
| 994 | |
| 995 | // destroyConstant - Remove the constant from the constant table. |
| 996 | // |
Owen Anderson | 0d2de8c | 2009-06-20 00:24:58 +0000 | [diff] [blame] | 997 | void UndefValue::destroyConstant() { |
Owen Anderson | 6179404 | 2009-06-17 20:10:08 +0000 | [diff] [blame] | 998 | // Implicitly locked. |
Owen Anderson | c8c3026 | 2009-07-31 22:45:43 +0000 | [diff] [blame] | 999 | getType()->getContext().pImpl->UndefValueConstants.remove(this); |
Chris Lattner | d5f67d8 | 2004-10-16 18:07:16 +0000 | [diff] [blame] | 1000 | destroyConstantImpl(); |
| 1001 | } |
| 1002 | |
Vikram S. Adve | 4e537b2 | 2002-07-14 23:13:17 +0000 | [diff] [blame] | 1003 | //---- ConstantExpr::get() implementations... |
Vikram S. Adve | 4e537b2 | 2002-07-14 23:13:17 +0000 | [diff] [blame] | 1004 | // |
Reid Spencer | 8d9336d | 2006-12-31 05:26:44 +0000 | [diff] [blame] | 1005 | |
Chris Lattner | 3e650af | 2004-08-04 04:48:01 +0000 | [diff] [blame] | 1006 | static ExprMapKeyType getValType(ConstantExpr *CE) { |
| 1007 | std::vector<Constant*> Operands; |
| 1008 | Operands.reserve(CE->getNumOperands()); |
| 1009 | for (unsigned i = 0, e = CE->getNumOperands(); i != e; ++i) |
| 1010 | Operands.push_back(cast<Constant>(CE->getOperand(i))); |
Reid Spencer | ee3c991 | 2006-12-04 05:19:50 +0000 | [diff] [blame] | 1011 | return ExprMapKeyType(CE->getOpcode(), Operands, |
Dan Gohman | 1ecaf45 | 2008-05-31 00:58:22 +0000 | [diff] [blame] | 1012 | CE->isCompare() ? CE->getPredicate() : 0, |
| 1013 | CE->hasIndices() ? |
| 1014 | CE->getIndices() : SmallVector<unsigned, 4>()); |
Chris Lattner | 3e650af | 2004-08-04 04:48:01 +0000 | [diff] [blame] | 1015 | } |
| 1016 | |
Reid Spencer | 6c38f0b | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 1017 | /// This is a utility function to handle folding of casts and lookup of the |
Duncan Sands | 7d6c8ae | 2008-03-30 19:38:55 +0000 | [diff] [blame] | 1018 | /// cast in the ExprConstants map. It is used by the various get* methods below. |
Reid Spencer | 6c38f0b | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 1019 | static inline Constant *getFoldedCast( |
Owen Anderson | 0d2de8c | 2009-06-20 00:24:58 +0000 | [diff] [blame] | 1020 | Instruction::CastOps opc, Constant *C, const Type *Ty) { |
Chris Lattner | 815ae2b | 2003-10-07 22:19:19 +0000 | [diff] [blame] | 1021 | assert(Ty->isFirstClassType() && "Cannot cast to an aggregate type!"); |
Reid Spencer | 6c38f0b | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 1022 | // Fold a few common cases |
Owen Anderson | 23a204d | 2009-07-31 17:39:07 +0000 | [diff] [blame] | 1023 | if (Constant *FC = ConstantFoldCastInstruction(Ty->getContext(), opc, C, Ty)) |
Reid Spencer | 6c38f0b | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 1024 | return FC; |
Chris Lattner | acdbe71 | 2003-04-17 19:24:48 +0000 | [diff] [blame] | 1025 | |
Owen Anderson | 1584a29 | 2009-08-04 20:25:11 +0000 | [diff] [blame] | 1026 | LLVMContextImpl *pImpl = Ty->getContext().pImpl; |
| 1027 | |
Vikram S. Adve | 4c48533 | 2002-07-15 18:19:33 +0000 | [diff] [blame] | 1028 | // Look up the constant in the table first to ensure uniqueness |
Chris Lattner | 2b383d2e | 2003-05-13 21:37:02 +0000 | [diff] [blame] | 1029 | std::vector<Constant*> argVec(1, C); |
Reid Spencer | ee3c991 | 2006-12-04 05:19:50 +0000 | [diff] [blame] | 1030 | ExprMapKeyType Key(opc, argVec); |
Owen Anderson | 2d7231d | 2009-06-17 18:40:29 +0000 | [diff] [blame] | 1031 | |
Owen Anderson | 6179404 | 2009-06-17 20:10:08 +0000 | [diff] [blame] | 1032 | // Implicitly locked. |
Owen Anderson | 1584a29 | 2009-08-04 20:25:11 +0000 | [diff] [blame] | 1033 | return pImpl->ExprConstants.getOrCreate(Ty, Key); |
Vikram S. Adve | 4e537b2 | 2002-07-14 23:13:17 +0000 | [diff] [blame] | 1034 | } |
Reid Spencer | f37dc65 | 2006-12-05 19:14:13 +0000 | [diff] [blame] | 1035 | |
Owen Anderson | 0d2de8c | 2009-06-20 00:24:58 +0000 | [diff] [blame] | 1036 | Constant *ConstantExpr::getCast(unsigned oc, Constant *C, const Type *Ty) { |
Reid Spencer | 6c38f0b | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 1037 | Instruction::CastOps opc = Instruction::CastOps(oc); |
| 1038 | assert(Instruction::isCast(opc) && "opcode out of range"); |
| 1039 | assert(C && Ty && "Null arguments to getCast"); |
| 1040 | assert(Ty->isFirstClassType() && "Cannot cast to an aggregate type!"); |
| 1041 | |
| 1042 | switch (opc) { |
| 1043 | default: |
Torok Edwin | fbcc663 | 2009-07-14 16:55:14 +0000 | [diff] [blame] | 1044 | llvm_unreachable("Invalid cast opcode"); |
Reid Spencer | 6c38f0b | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 1045 | break; |
Owen Anderson | 0d2de8c | 2009-06-20 00:24:58 +0000 | [diff] [blame] | 1046 | case Instruction::Trunc: return getTrunc(C, Ty); |
| 1047 | case Instruction::ZExt: return getZExt(C, Ty); |
| 1048 | case Instruction::SExt: return getSExt(C, Ty); |
| 1049 | case Instruction::FPTrunc: return getFPTrunc(C, Ty); |
| 1050 | case Instruction::FPExt: return getFPExtend(C, Ty); |
| 1051 | case Instruction::UIToFP: return getUIToFP(C, Ty); |
| 1052 | case Instruction::SIToFP: return getSIToFP(C, Ty); |
| 1053 | case Instruction::FPToUI: return getFPToUI(C, Ty); |
| 1054 | case Instruction::FPToSI: return getFPToSI(C, Ty); |
| 1055 | case Instruction::PtrToInt: return getPtrToInt(C, Ty); |
| 1056 | case Instruction::IntToPtr: return getIntToPtr(C, Ty); |
| 1057 | case Instruction::BitCast: return getBitCast(C, Ty); |
Chris Lattner | 1ece6f8 | 2005-01-01 15:59:57 +0000 | [diff] [blame] | 1058 | } |
Reid Spencer | 6c38f0b | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 1059 | return 0; |
Reid Spencer | f37dc65 | 2006-12-05 19:14:13 +0000 | [diff] [blame] | 1060 | } |
| 1061 | |
Reid Spencer | 5c14088 | 2006-12-04 20:17:56 +0000 | [diff] [blame] | 1062 | Constant *ConstantExpr::getZExtOrBitCast(Constant *C, const Type *Ty) { |
Dan Gohman | 7ccc52f | 2009-06-15 22:12:54 +0000 | [diff] [blame] | 1063 | if (C->getType()->getScalarSizeInBits() == Ty->getScalarSizeInBits()) |
Reid Spencer | 5c14088 | 2006-12-04 20:17:56 +0000 | [diff] [blame] | 1064 | return getCast(Instruction::BitCast, C, Ty); |
| 1065 | return getCast(Instruction::ZExt, C, Ty); |
| 1066 | } |
| 1067 | |
Owen Anderson | 0d2de8c | 2009-06-20 00:24:58 +0000 | [diff] [blame] | 1068 | Constant *ConstantExpr::getSExtOrBitCast(Constant *C, const Type *Ty) { |
Dan Gohman | 7ccc52f | 2009-06-15 22:12:54 +0000 | [diff] [blame] | 1069 | if (C->getType()->getScalarSizeInBits() == Ty->getScalarSizeInBits()) |
Owen Anderson | 0d2de8c | 2009-06-20 00:24:58 +0000 | [diff] [blame] | 1070 | return getCast(Instruction::BitCast, C, Ty); |
| 1071 | return getCast(Instruction::SExt, C, Ty); |
Reid Spencer | 5c14088 | 2006-12-04 20:17:56 +0000 | [diff] [blame] | 1072 | } |
| 1073 | |
| 1074 | Constant *ConstantExpr::getTruncOrBitCast(Constant *C, const Type *Ty) { |
Dan Gohman | 7ccc52f | 2009-06-15 22:12:54 +0000 | [diff] [blame] | 1075 | if (C->getType()->getScalarSizeInBits() == Ty->getScalarSizeInBits()) |
Reid Spencer | 5c14088 | 2006-12-04 20:17:56 +0000 | [diff] [blame] | 1076 | return getCast(Instruction::BitCast, C, Ty); |
| 1077 | return getCast(Instruction::Trunc, C, Ty); |
| 1078 | } |
| 1079 | |
Owen Anderson | 0d2de8c | 2009-06-20 00:24:58 +0000 | [diff] [blame] | 1080 | Constant *ConstantExpr::getPointerCast(Constant *S, const Type *Ty) { |
Reid Spencer | bc245a0 | 2006-12-05 03:25:26 +0000 | [diff] [blame] | 1081 | assert(isa<PointerType>(S->getType()) && "Invalid cast"); |
Chris Lattner | 03c4953 | 2007-01-15 02:27:26 +0000 | [diff] [blame] | 1082 | assert((Ty->isInteger() || isa<PointerType>(Ty)) && "Invalid cast"); |
Reid Spencer | bc245a0 | 2006-12-05 03:25:26 +0000 | [diff] [blame] | 1083 | |
Chris Lattner | 03c4953 | 2007-01-15 02:27:26 +0000 | [diff] [blame] | 1084 | if (Ty->isInteger()) |
Owen Anderson | 0d2de8c | 2009-06-20 00:24:58 +0000 | [diff] [blame] | 1085 | return getCast(Instruction::PtrToInt, S, Ty); |
| 1086 | return getCast(Instruction::BitCast, S, Ty); |
Reid Spencer | bc245a0 | 2006-12-05 03:25:26 +0000 | [diff] [blame] | 1087 | } |
| 1088 | |
Reid Spencer | 56521c4 | 2006-12-12 00:51:07 +0000 | [diff] [blame] | 1089 | Constant *ConstantExpr::getIntegerCast(Constant *C, const Type *Ty, |
| 1090 | bool isSigned) { |
Dan Gohman | 7ccc52f | 2009-06-15 22:12:54 +0000 | [diff] [blame] | 1091 | assert(C->getType()->isIntOrIntVector() && |
| 1092 | Ty->isIntOrIntVector() && "Invalid cast"); |
| 1093 | unsigned SrcBits = C->getType()->getScalarSizeInBits(); |
| 1094 | unsigned DstBits = Ty->getScalarSizeInBits(); |
Reid Spencer | 56521c4 | 2006-12-12 00:51:07 +0000 | [diff] [blame] | 1095 | Instruction::CastOps opcode = |
| 1096 | (SrcBits == DstBits ? Instruction::BitCast : |
| 1097 | (SrcBits > DstBits ? Instruction::Trunc : |
| 1098 | (isSigned ? Instruction::SExt : Instruction::ZExt))); |
| 1099 | return getCast(opcode, C, Ty); |
| 1100 | } |
| 1101 | |
| 1102 | Constant *ConstantExpr::getFPCast(Constant *C, const Type *Ty) { |
Dan Gohman | 7ccc52f | 2009-06-15 22:12:54 +0000 | [diff] [blame] | 1103 | assert(C->getType()->isFPOrFPVector() && Ty->isFPOrFPVector() && |
Reid Spencer | 56521c4 | 2006-12-12 00:51:07 +0000 | [diff] [blame] | 1104 | "Invalid cast"); |
Dan Gohman | 7ccc52f | 2009-06-15 22:12:54 +0000 | [diff] [blame] | 1105 | unsigned SrcBits = C->getType()->getScalarSizeInBits(); |
| 1106 | unsigned DstBits = Ty->getScalarSizeInBits(); |
Reid Spencer | ca104e8 | 2006-12-12 05:38:50 +0000 | [diff] [blame] | 1107 | if (SrcBits == DstBits) |
| 1108 | return C; // Avoid a useless cast |
Reid Spencer | 56521c4 | 2006-12-12 00:51:07 +0000 | [diff] [blame] | 1109 | Instruction::CastOps opcode = |
Reid Spencer | ca104e8 | 2006-12-12 05:38:50 +0000 | [diff] [blame] | 1110 | (SrcBits > DstBits ? Instruction::FPTrunc : Instruction::FPExt); |
Reid Spencer | 56521c4 | 2006-12-12 00:51:07 +0000 | [diff] [blame] | 1111 | return getCast(opcode, C, Ty); |
| 1112 | } |
| 1113 | |
Owen Anderson | 0d2de8c | 2009-06-20 00:24:58 +0000 | [diff] [blame] | 1114 | Constant *ConstantExpr::getTrunc(Constant *C, const Type *Ty) { |
Dan Gohman | 7ccc52f | 2009-06-15 22:12:54 +0000 | [diff] [blame] | 1115 | #ifndef NDEBUG |
| 1116 | bool fromVec = C->getType()->getTypeID() == Type::VectorTyID; |
| 1117 | bool toVec = Ty->getTypeID() == Type::VectorTyID; |
| 1118 | #endif |
| 1119 | assert((fromVec == toVec) && "Cannot convert from scalar to/from vector"); |
| 1120 | assert(C->getType()->isIntOrIntVector() && "Trunc operand must be integer"); |
| 1121 | assert(Ty->isIntOrIntVector() && "Trunc produces only integral"); |
| 1122 | assert(C->getType()->getScalarSizeInBits() > Ty->getScalarSizeInBits()&& |
Reid Spencer | 6c38f0b | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 1123 | "SrcTy must be larger than DestTy for Trunc!"); |
| 1124 | |
Owen Anderson | 0d2de8c | 2009-06-20 00:24:58 +0000 | [diff] [blame] | 1125 | return getFoldedCast(Instruction::Trunc, C, Ty); |
Reid Spencer | 6c38f0b | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 1126 | } |
| 1127 | |
Owen Anderson | 0d2de8c | 2009-06-20 00:24:58 +0000 | [diff] [blame] | 1128 | Constant *ConstantExpr::getSExt(Constant *C, const Type *Ty) { |
Dan Gohman | 7ccc52f | 2009-06-15 22:12:54 +0000 | [diff] [blame] | 1129 | #ifndef NDEBUG |
| 1130 | bool fromVec = C->getType()->getTypeID() == Type::VectorTyID; |
| 1131 | bool toVec = Ty->getTypeID() == Type::VectorTyID; |
| 1132 | #endif |
| 1133 | assert((fromVec == toVec) && "Cannot convert from scalar to/from vector"); |
| 1134 | assert(C->getType()->isIntOrIntVector() && "SExt operand must be integral"); |
| 1135 | assert(Ty->isIntOrIntVector() && "SExt produces only integer"); |
| 1136 | assert(C->getType()->getScalarSizeInBits() < Ty->getScalarSizeInBits()&& |
Reid Spencer | 6c38f0b | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 1137 | "SrcTy must be smaller than DestTy for SExt!"); |
| 1138 | |
Owen Anderson | 0d2de8c | 2009-06-20 00:24:58 +0000 | [diff] [blame] | 1139 | return getFoldedCast(Instruction::SExt, C, Ty); |
Chris Lattner | dd28474 | 2004-04-04 23:20:30 +0000 | [diff] [blame] | 1140 | } |
| 1141 | |
Owen Anderson | 0d2de8c | 2009-06-20 00:24:58 +0000 | [diff] [blame] | 1142 | Constant *ConstantExpr::getZExt(Constant *C, const Type *Ty) { |
Dan Gohman | 7ccc52f | 2009-06-15 22:12:54 +0000 | [diff] [blame] | 1143 | #ifndef NDEBUG |
| 1144 | bool fromVec = C->getType()->getTypeID() == Type::VectorTyID; |
| 1145 | bool toVec = Ty->getTypeID() == Type::VectorTyID; |
| 1146 | #endif |
| 1147 | assert((fromVec == toVec) && "Cannot convert from scalar to/from vector"); |
| 1148 | assert(C->getType()->isIntOrIntVector() && "ZEXt operand must be integral"); |
| 1149 | assert(Ty->isIntOrIntVector() && "ZExt produces only integer"); |
| 1150 | assert(C->getType()->getScalarSizeInBits() < Ty->getScalarSizeInBits()&& |
Reid Spencer | 6c38f0b | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 1151 | "SrcTy must be smaller than DestTy for ZExt!"); |
| 1152 | |
Owen Anderson | 0d2de8c | 2009-06-20 00:24:58 +0000 | [diff] [blame] | 1153 | return getFoldedCast(Instruction::ZExt, C, Ty); |
Reid Spencer | 6c38f0b | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 1154 | } |
| 1155 | |
Owen Anderson | 0d2de8c | 2009-06-20 00:24:58 +0000 | [diff] [blame] | 1156 | Constant *ConstantExpr::getFPTrunc(Constant *C, const Type *Ty) { |
Dan Gohman | 7ccc52f | 2009-06-15 22:12:54 +0000 | [diff] [blame] | 1157 | #ifndef NDEBUG |
| 1158 | bool fromVec = C->getType()->getTypeID() == Type::VectorTyID; |
| 1159 | bool toVec = Ty->getTypeID() == Type::VectorTyID; |
| 1160 | #endif |
| 1161 | assert((fromVec == toVec) && "Cannot convert from scalar to/from vector"); |
| 1162 | assert(C->getType()->isFPOrFPVector() && Ty->isFPOrFPVector() && |
| 1163 | C->getType()->getScalarSizeInBits() > Ty->getScalarSizeInBits()&& |
Reid Spencer | 6c38f0b | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 1164 | "This is an illegal floating point truncation!"); |
Owen Anderson | 0d2de8c | 2009-06-20 00:24:58 +0000 | [diff] [blame] | 1165 | return getFoldedCast(Instruction::FPTrunc, C, Ty); |
Reid Spencer | 6c38f0b | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 1166 | } |
| 1167 | |
Owen Anderson | 0d2de8c | 2009-06-20 00:24:58 +0000 | [diff] [blame] | 1168 | Constant *ConstantExpr::getFPExtend(Constant *C, const Type *Ty) { |
Dan Gohman | 7ccc52f | 2009-06-15 22:12:54 +0000 | [diff] [blame] | 1169 | #ifndef NDEBUG |
| 1170 | bool fromVec = C->getType()->getTypeID() == Type::VectorTyID; |
| 1171 | bool toVec = Ty->getTypeID() == Type::VectorTyID; |
| 1172 | #endif |
| 1173 | assert((fromVec == toVec) && "Cannot convert from scalar to/from vector"); |
| 1174 | assert(C->getType()->isFPOrFPVector() && Ty->isFPOrFPVector() && |
| 1175 | C->getType()->getScalarSizeInBits() < Ty->getScalarSizeInBits()&& |
Reid Spencer | 6c38f0b | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 1176 | "This is an illegal floating point extension!"); |
Owen Anderson | 0d2de8c | 2009-06-20 00:24:58 +0000 | [diff] [blame] | 1177 | return getFoldedCast(Instruction::FPExt, C, Ty); |
Reid Spencer | 6c38f0b | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 1178 | } |
| 1179 | |
Owen Anderson | 0d2de8c | 2009-06-20 00:24:58 +0000 | [diff] [blame] | 1180 | Constant *ConstantExpr::getUIToFP(Constant *C, const Type *Ty) { |
Devang Patel | d26344d | 2008-11-03 23:20:04 +0000 | [diff] [blame] | 1181 | #ifndef NDEBUG |
Nate Begeman | d4d45c2 | 2007-11-17 03:58:34 +0000 | [diff] [blame] | 1182 | bool fromVec = C->getType()->getTypeID() == Type::VectorTyID; |
| 1183 | bool toVec = Ty->getTypeID() == Type::VectorTyID; |
Devang Patel | d26344d | 2008-11-03 23:20:04 +0000 | [diff] [blame] | 1184 | #endif |
Nate Begeman | d4d45c2 | 2007-11-17 03:58:34 +0000 | [diff] [blame] | 1185 | assert((fromVec == toVec) && "Cannot convert from scalar to/from vector"); |
| 1186 | assert(C->getType()->isIntOrIntVector() && Ty->isFPOrFPVector() && |
| 1187 | "This is an illegal uint to floating point cast!"); |
Owen Anderson | 0d2de8c | 2009-06-20 00:24:58 +0000 | [diff] [blame] | 1188 | return getFoldedCast(Instruction::UIToFP, C, Ty); |
Reid Spencer | 6c38f0b | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 1189 | } |
| 1190 | |
Owen Anderson | 0d2de8c | 2009-06-20 00:24:58 +0000 | [diff] [blame] | 1191 | Constant *ConstantExpr::getSIToFP(Constant *C, const Type *Ty) { |
Devang Patel | d26344d | 2008-11-03 23:20:04 +0000 | [diff] [blame] | 1192 | #ifndef NDEBUG |
Nate Begeman | d4d45c2 | 2007-11-17 03:58:34 +0000 | [diff] [blame] | 1193 | bool fromVec = C->getType()->getTypeID() == Type::VectorTyID; |
| 1194 | bool toVec = Ty->getTypeID() == Type::VectorTyID; |
Devang Patel | d26344d | 2008-11-03 23:20:04 +0000 | [diff] [blame] | 1195 | #endif |
Nate Begeman | d4d45c2 | 2007-11-17 03:58:34 +0000 | [diff] [blame] | 1196 | assert((fromVec == toVec) && "Cannot convert from scalar to/from vector"); |
| 1197 | assert(C->getType()->isIntOrIntVector() && Ty->isFPOrFPVector() && |
Reid Spencer | 6c38f0b | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 1198 | "This is an illegal sint to floating point cast!"); |
Owen Anderson | 0d2de8c | 2009-06-20 00:24:58 +0000 | [diff] [blame] | 1199 | return getFoldedCast(Instruction::SIToFP, C, Ty); |
Reid Spencer | 6c38f0b | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 1200 | } |
| 1201 | |
Owen Anderson | 0d2de8c | 2009-06-20 00:24:58 +0000 | [diff] [blame] | 1202 | Constant *ConstantExpr::getFPToUI(Constant *C, const Type *Ty) { |
Devang Patel | d26344d | 2008-11-03 23:20:04 +0000 | [diff] [blame] | 1203 | #ifndef NDEBUG |
Nate Begeman | d4d45c2 | 2007-11-17 03:58:34 +0000 | [diff] [blame] | 1204 | bool fromVec = C->getType()->getTypeID() == Type::VectorTyID; |
| 1205 | bool toVec = Ty->getTypeID() == Type::VectorTyID; |
Devang Patel | d26344d | 2008-11-03 23:20:04 +0000 | [diff] [blame] | 1206 | #endif |
Nate Begeman | d4d45c2 | 2007-11-17 03:58:34 +0000 | [diff] [blame] | 1207 | assert((fromVec == toVec) && "Cannot convert from scalar to/from vector"); |
| 1208 | assert(C->getType()->isFPOrFPVector() && Ty->isIntOrIntVector() && |
| 1209 | "This is an illegal floating point to uint cast!"); |
Owen Anderson | 0d2de8c | 2009-06-20 00:24:58 +0000 | [diff] [blame] | 1210 | return getFoldedCast(Instruction::FPToUI, C, Ty); |
Reid Spencer | 6c38f0b | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 1211 | } |
| 1212 | |
Owen Anderson | 0d2de8c | 2009-06-20 00:24:58 +0000 | [diff] [blame] | 1213 | Constant *ConstantExpr::getFPToSI(Constant *C, const Type *Ty) { |
Devang Patel | d26344d | 2008-11-03 23:20:04 +0000 | [diff] [blame] | 1214 | #ifndef NDEBUG |
Nate Begeman | d4d45c2 | 2007-11-17 03:58:34 +0000 | [diff] [blame] | 1215 | bool fromVec = C->getType()->getTypeID() == Type::VectorTyID; |
| 1216 | bool toVec = Ty->getTypeID() == Type::VectorTyID; |
Devang Patel | d26344d | 2008-11-03 23:20:04 +0000 | [diff] [blame] | 1217 | #endif |
Nate Begeman | d4d45c2 | 2007-11-17 03:58:34 +0000 | [diff] [blame] | 1218 | assert((fromVec == toVec) && "Cannot convert from scalar to/from vector"); |
| 1219 | assert(C->getType()->isFPOrFPVector() && Ty->isIntOrIntVector() && |
| 1220 | "This is an illegal floating point to sint cast!"); |
Owen Anderson | 0d2de8c | 2009-06-20 00:24:58 +0000 | [diff] [blame] | 1221 | return getFoldedCast(Instruction::FPToSI, C, Ty); |
Reid Spencer | 6c38f0b | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 1222 | } |
| 1223 | |
Owen Anderson | 0d2de8c | 2009-06-20 00:24:58 +0000 | [diff] [blame] | 1224 | Constant *ConstantExpr::getPtrToInt(Constant *C, const Type *DstTy) { |
Reid Spencer | 6c38f0b | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 1225 | assert(isa<PointerType>(C->getType()) && "PtrToInt source must be pointer"); |
Chris Lattner | 03c4953 | 2007-01-15 02:27:26 +0000 | [diff] [blame] | 1226 | assert(DstTy->isInteger() && "PtrToInt destination must be integral"); |
Owen Anderson | 0d2de8c | 2009-06-20 00:24:58 +0000 | [diff] [blame] | 1227 | return getFoldedCast(Instruction::PtrToInt, C, DstTy); |
Reid Spencer | 6c38f0b | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 1228 | } |
| 1229 | |
Owen Anderson | 0d2de8c | 2009-06-20 00:24:58 +0000 | [diff] [blame] | 1230 | Constant *ConstantExpr::getIntToPtr(Constant *C, const Type *DstTy) { |
Chris Lattner | 03c4953 | 2007-01-15 02:27:26 +0000 | [diff] [blame] | 1231 | assert(C->getType()->isInteger() && "IntToPtr source must be integral"); |
Reid Spencer | 6c38f0b | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 1232 | assert(isa<PointerType>(DstTy) && "IntToPtr destination must be a pointer"); |
Owen Anderson | 0d2de8c | 2009-06-20 00:24:58 +0000 | [diff] [blame] | 1233 | return getFoldedCast(Instruction::IntToPtr, C, DstTy); |
Reid Spencer | 6c38f0b | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 1234 | } |
| 1235 | |
Owen Anderson | 0d2de8c | 2009-06-20 00:24:58 +0000 | [diff] [blame] | 1236 | Constant *ConstantExpr::getBitCast(Constant *C, const Type *DstTy) { |
Reid Spencer | 6c38f0b | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 1237 | // BitCast implies a no-op cast of type only. No bits change. However, you |
| 1238 | // can't cast pointers to anything but pointers. |
Devang Patel | d26344d | 2008-11-03 23:20:04 +0000 | [diff] [blame] | 1239 | #ifndef NDEBUG |
Reid Spencer | 6c38f0b | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 1240 | const Type *SrcTy = C->getType(); |
| 1241 | assert((isa<PointerType>(SrcTy) == isa<PointerType>(DstTy)) && |
Reid Spencer | 5c14088 | 2006-12-04 20:17:56 +0000 | [diff] [blame] | 1242 | "BitCast cannot cast pointer to non-pointer and vice versa"); |
Reid Spencer | 6c38f0b | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 1243 | |
| 1244 | // Now we know we're not dealing with mismatched pointer casts (ptr->nonptr |
| 1245 | // or nonptr->ptr). For all the other types, the cast is okay if source and |
| 1246 | // destination bit widths are identical. |
| 1247 | unsigned SrcBitSize = SrcTy->getPrimitiveSizeInBits(); |
| 1248 | unsigned DstBitSize = DstTy->getPrimitiveSizeInBits(); |
Devang Patel | d26344d | 2008-11-03 23:20:04 +0000 | [diff] [blame] | 1249 | #endif |
Chris Lattner | e408601 | 2009-03-08 04:06:26 +0000 | [diff] [blame] | 1250 | assert(SrcBitSize == DstBitSize && "BitCast requires types of same width"); |
Chris Lattner | cbeda87 | 2009-03-21 06:55:54 +0000 | [diff] [blame] | 1251 | |
| 1252 | // It is common to ask for a bitcast of a value to its own type, handle this |
| 1253 | // speedily. |
| 1254 | if (C->getType() == DstTy) return C; |
| 1255 | |
Owen Anderson | 0d2de8c | 2009-06-20 00:24:58 +0000 | [diff] [blame] | 1256 | return getFoldedCast(Instruction::BitCast, C, DstTy); |
Chris Lattner | dd28474 | 2004-04-04 23:20:30 +0000 | [diff] [blame] | 1257 | } |
| 1258 | |
Chris Lattner | b50d135 | 2003-10-05 00:17:43 +0000 | [diff] [blame] | 1259 | Constant *ConstantExpr::getTy(const Type *ReqTy, unsigned Opcode, |
Owen Anderson | 0d2de8c | 2009-06-20 00:24:58 +0000 | [diff] [blame] | 1260 | Constant *C1, Constant *C2) { |
Chris Lattner | 38a9bcd | 2003-05-21 17:49:25 +0000 | [diff] [blame] | 1261 | // Check the operands for consistency first |
Reid Spencer | 7eb55b3 | 2006-11-02 01:53:59 +0000 | [diff] [blame] | 1262 | assert(Opcode >= Instruction::BinaryOpsBegin && |
| 1263 | Opcode < Instruction::BinaryOpsEnd && |
Chris Lattner | 38a9bcd | 2003-05-21 17:49:25 +0000 | [diff] [blame] | 1264 | "Invalid opcode in binary constant expression"); |
| 1265 | assert(C1->getType() == C2->getType() && |
| 1266 | "Operand types in binary constant expression should match"); |
Chris Lattner | b50d135 | 2003-10-05 00:17:43 +0000 | [diff] [blame] | 1267 | |
Owen Anderson | 55f1c09 | 2009-08-13 21:58:54 +0000 | [diff] [blame] | 1268 | if (ReqTy == C1->getType() || ReqTy == Type::getInt1Ty(ReqTy->getContext())) |
Owen Anderson | 23a204d | 2009-07-31 17:39:07 +0000 | [diff] [blame] | 1269 | if (Constant *FC = ConstantFoldBinaryInstruction(ReqTy->getContext(), |
| 1270 | Opcode, C1, C2)) |
Chris Lattner | b50d135 | 2003-10-05 00:17:43 +0000 | [diff] [blame] | 1271 | return FC; // Fold a few common cases... |
Chris Lattner | acdbe71 | 2003-04-17 19:24:48 +0000 | [diff] [blame] | 1272 | |
Chris Lattner | 2b383d2e | 2003-05-13 21:37:02 +0000 | [diff] [blame] | 1273 | std::vector<Constant*> argVec(1, C1); argVec.push_back(C2); |
Reid Spencer | a009d0d | 2006-12-04 21:35:24 +0000 | [diff] [blame] | 1274 | ExprMapKeyType Key(Opcode, argVec); |
Owen Anderson | 6179404 | 2009-06-17 20:10:08 +0000 | [diff] [blame] | 1275 | |
Owen Anderson | 1584a29 | 2009-08-04 20:25:11 +0000 | [diff] [blame] | 1276 | LLVMContextImpl *pImpl = ReqTy->getContext().pImpl; |
| 1277 | |
Owen Anderson | 0d2de8c | 2009-06-20 00:24:58 +0000 | [diff] [blame] | 1278 | // Implicitly locked. |
Owen Anderson | 1584a29 | 2009-08-04 20:25:11 +0000 | [diff] [blame] | 1279 | return pImpl->ExprConstants.getOrCreate(ReqTy, Key); |
Vikram S. Adve | 4e537b2 | 2002-07-14 23:13:17 +0000 | [diff] [blame] | 1280 | } |
| 1281 | |
Reid Spencer | 266e42b | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 1282 | Constant *ConstantExpr::getCompareTy(unsigned short predicate, |
Nate Begeman | 098cc6f | 2008-07-25 17:56:27 +0000 | [diff] [blame] | 1283 | Constant *C1, Constant *C2) { |
Reid Spencer | 266e42b | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 1284 | switch (predicate) { |
Torok Edwin | fbcc663 | 2009-07-14 16:55:14 +0000 | [diff] [blame] | 1285 | default: llvm_unreachable("Invalid CmpInst predicate"); |
Nate Begeman | c96e2e4 | 2008-07-25 17:35:37 +0000 | [diff] [blame] | 1286 | case CmpInst::FCMP_FALSE: case CmpInst::FCMP_OEQ: case CmpInst::FCMP_OGT: |
| 1287 | case CmpInst::FCMP_OGE: case CmpInst::FCMP_OLT: case CmpInst::FCMP_OLE: |
| 1288 | case CmpInst::FCMP_ONE: case CmpInst::FCMP_ORD: case CmpInst::FCMP_UNO: |
| 1289 | case CmpInst::FCMP_UEQ: case CmpInst::FCMP_UGT: case CmpInst::FCMP_UGE: |
| 1290 | case CmpInst::FCMP_ULT: case CmpInst::FCMP_ULE: case CmpInst::FCMP_UNE: |
| 1291 | case CmpInst::FCMP_TRUE: |
Nick Lewycky | a21d3da | 2009-07-08 03:04:38 +0000 | [diff] [blame] | 1292 | return getFCmp(predicate, C1, C2); |
| 1293 | |
Nate Begeman | c96e2e4 | 2008-07-25 17:35:37 +0000 | [diff] [blame] | 1294 | case CmpInst::ICMP_EQ: case CmpInst::ICMP_NE: case CmpInst::ICMP_UGT: |
| 1295 | case CmpInst::ICMP_UGE: case CmpInst::ICMP_ULT: case CmpInst::ICMP_ULE: |
| 1296 | case CmpInst::ICMP_SGT: case CmpInst::ICMP_SGE: case CmpInst::ICMP_SLT: |
| 1297 | case CmpInst::ICMP_SLE: |
Nick Lewycky | a21d3da | 2009-07-08 03:04:38 +0000 | [diff] [blame] | 1298 | return getICmp(predicate, C1, C2); |
Reid Spencer | 266e42b | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 1299 | } |
Reid Spencer | a009d0d | 2006-12-04 21:35:24 +0000 | [diff] [blame] | 1300 | } |
| 1301 | |
Owen Anderson | 0d2de8c | 2009-06-20 00:24:58 +0000 | [diff] [blame] | 1302 | Constant *ConstantExpr::get(unsigned Opcode, Constant *C1, Constant *C2) { |
Dan Gohman | a5b9645 | 2009-06-04 22:49:04 +0000 | [diff] [blame] | 1303 | // API compatibility: Adjust integer opcodes to floating-point opcodes. |
| 1304 | if (C1->getType()->isFPOrFPVector()) { |
| 1305 | if (Opcode == Instruction::Add) Opcode = Instruction::FAdd; |
| 1306 | else if (Opcode == Instruction::Sub) Opcode = Instruction::FSub; |
| 1307 | else if (Opcode == Instruction::Mul) Opcode = Instruction::FMul; |
| 1308 | } |
Chris Lattner | caf3f3e | 2004-08-17 17:28:46 +0000 | [diff] [blame] | 1309 | #ifndef NDEBUG |
| 1310 | switch (Opcode) { |
Dan Gohman | a5b9645 | 2009-06-04 22:49:04 +0000 | [diff] [blame] | 1311 | case Instruction::Add: |
Reid Spencer | 7eb55b3 | 2006-11-02 01:53:59 +0000 | [diff] [blame] | 1312 | case Instruction::Sub: |
Dan Gohman | a5b9645 | 2009-06-04 22:49:04 +0000 | [diff] [blame] | 1313 | case Instruction::Mul: |
Chris Lattner | caf3f3e | 2004-08-17 17:28:46 +0000 | [diff] [blame] | 1314 | assert(C1->getType() == C2->getType() && "Op types should be identical!"); |
Dan Gohman | a5b9645 | 2009-06-04 22:49:04 +0000 | [diff] [blame] | 1315 | assert(C1->getType()->isIntOrIntVector() && |
| 1316 | "Tried to create an integer operation on a non-integer type!"); |
| 1317 | break; |
| 1318 | case Instruction::FAdd: |
| 1319 | case Instruction::FSub: |
| 1320 | case Instruction::FMul: |
| 1321 | assert(C1->getType() == C2->getType() && "Op types should be identical!"); |
| 1322 | assert(C1->getType()->isFPOrFPVector() && |
| 1323 | "Tried to create a floating-point operation on a " |
| 1324 | "non-floating-point type!"); |
Chris Lattner | caf3f3e | 2004-08-17 17:28:46 +0000 | [diff] [blame] | 1325 | break; |
Reid Spencer | 7e80b0b | 2006-10-26 06:15:43 +0000 | [diff] [blame] | 1326 | case Instruction::UDiv: |
| 1327 | case Instruction::SDiv: |
| 1328 | assert(C1->getType() == C2->getType() && "Op types should be identical!"); |
Dan Gohman | 7889f2b | 2009-06-15 22:25:12 +0000 | [diff] [blame] | 1329 | assert(C1->getType()->isIntOrIntVector() && |
Reid Spencer | 7e80b0b | 2006-10-26 06:15:43 +0000 | [diff] [blame] | 1330 | "Tried to create an arithmetic operation on a non-arithmetic type!"); |
| 1331 | break; |
| 1332 | case Instruction::FDiv: |
| 1333 | assert(C1->getType() == C2->getType() && "Op types should be identical!"); |
Dan Gohman | 7889f2b | 2009-06-15 22:25:12 +0000 | [diff] [blame] | 1334 | assert(C1->getType()->isFPOrFPVector() && |
| 1335 | "Tried to create an arithmetic operation on a non-arithmetic type!"); |
Reid Spencer | 7e80b0b | 2006-10-26 06:15:43 +0000 | [diff] [blame] | 1336 | break; |
Reid Spencer | 7eb55b3 | 2006-11-02 01:53:59 +0000 | [diff] [blame] | 1337 | case Instruction::URem: |
| 1338 | case Instruction::SRem: |
| 1339 | assert(C1->getType() == C2->getType() && "Op types should be identical!"); |
Dan Gohman | 7889f2b | 2009-06-15 22:25:12 +0000 | [diff] [blame] | 1340 | assert(C1->getType()->isIntOrIntVector() && |
Reid Spencer | 7eb55b3 | 2006-11-02 01:53:59 +0000 | [diff] [blame] | 1341 | "Tried to create an arithmetic operation on a non-arithmetic type!"); |
| 1342 | break; |
| 1343 | case Instruction::FRem: |
| 1344 | assert(C1->getType() == C2->getType() && "Op types should be identical!"); |
Dan Gohman | 7889f2b | 2009-06-15 22:25:12 +0000 | [diff] [blame] | 1345 | assert(C1->getType()->isFPOrFPVector() && |
| 1346 | "Tried to create an arithmetic operation on a non-arithmetic type!"); |
Reid Spencer | 7eb55b3 | 2006-11-02 01:53:59 +0000 | [diff] [blame] | 1347 | break; |
Chris Lattner | caf3f3e | 2004-08-17 17:28:46 +0000 | [diff] [blame] | 1348 | case Instruction::And: |
| 1349 | case Instruction::Or: |
| 1350 | case Instruction::Xor: |
| 1351 | assert(C1->getType() == C2->getType() && "Op types should be identical!"); |
Dan Gohman | 7889f2b | 2009-06-15 22:25:12 +0000 | [diff] [blame] | 1352 | assert(C1->getType()->isIntOrIntVector() && |
Misha Brukman | 3852f65 | 2005-01-27 06:46:38 +0000 | [diff] [blame] | 1353 | "Tried to create a logical operation on a non-integral type!"); |
Chris Lattner | caf3f3e | 2004-08-17 17:28:46 +0000 | [diff] [blame] | 1354 | break; |
Chris Lattner | caf3f3e | 2004-08-17 17:28:46 +0000 | [diff] [blame] | 1355 | case Instruction::Shl: |
Reid Spencer | fdff938 | 2006-11-08 06:47:33 +0000 | [diff] [blame] | 1356 | case Instruction::LShr: |
| 1357 | case Instruction::AShr: |
Reid Spencer | 2341c22 | 2007-02-02 02:16:23 +0000 | [diff] [blame] | 1358 | assert(C1->getType() == C2->getType() && "Op types should be identical!"); |
Dan Gohman | 79975d5 | 2009-03-14 17:09:17 +0000 | [diff] [blame] | 1359 | assert(C1->getType()->isIntOrIntVector() && |
Chris Lattner | caf3f3e | 2004-08-17 17:28:46 +0000 | [diff] [blame] | 1360 | "Tried to create a shift operation on a non-integer type!"); |
| 1361 | break; |
| 1362 | default: |
| 1363 | break; |
| 1364 | } |
| 1365 | #endif |
| 1366 | |
Owen Anderson | 0d2de8c | 2009-06-20 00:24:58 +0000 | [diff] [blame] | 1367 | return getTy(C1->getType(), Opcode, C1, C2); |
Reid Spencer | a009d0d | 2006-12-04 21:35:24 +0000 | [diff] [blame] | 1368 | } |
| 1369 | |
Owen Anderson | 487375e | 2009-07-29 18:55:55 +0000 | [diff] [blame] | 1370 | Constant* ConstantExpr::getSizeOf(const Type* Ty) { |
| 1371 | // sizeof is implemented as: (i64) gep (Ty*)null, 1 |
| 1372 | // Note that a non-inbounds gep is used, as null isn't within any object. |
Owen Anderson | 55f1c09 | 2009-08-13 21:58:54 +0000 | [diff] [blame] | 1373 | Constant *GEPIdx = ConstantInt::get(Type::getInt32Ty(Ty->getContext()), 1); |
Owen Anderson | 487375e | 2009-07-29 18:55:55 +0000 | [diff] [blame] | 1374 | Constant *GEP = getGetElementPtr( |
Owen Anderson | 5a1acd9 | 2009-07-31 20:28:14 +0000 | [diff] [blame] | 1375 | Constant::getNullValue(PointerType::getUnqual(Ty)), &GEPIdx, 1); |
Owen Anderson | 55f1c09 | 2009-08-13 21:58:54 +0000 | [diff] [blame] | 1376 | return getCast(Instruction::PtrToInt, GEP, |
| 1377 | Type::getInt64Ty(Ty->getContext())); |
Owen Anderson | 487375e | 2009-07-29 18:55:55 +0000 | [diff] [blame] | 1378 | } |
| 1379 | |
| 1380 | Constant* ConstantExpr::getAlignOf(const Type* Ty) { |
Owen Anderson | 487375e | 2009-07-29 18:55:55 +0000 | [diff] [blame] | 1381 | // alignof is implemented as: (i64) gep ({i8,Ty}*)null, 0, 1 |
Dan Gohman | 50c09d0 | 2009-08-11 17:57:01 +0000 | [diff] [blame] | 1382 | // Note that a non-inbounds gep is used, as null isn't within any object. |
Owen Anderson | 03cb69f | 2009-08-05 23:16:16 +0000 | [diff] [blame] | 1383 | const Type *AligningTy = StructType::get(Ty->getContext(), |
Owen Anderson | 55f1c09 | 2009-08-13 21:58:54 +0000 | [diff] [blame] | 1384 | Type::getInt8Ty(Ty->getContext()), Ty, NULL); |
Owen Anderson | 5a1acd9 | 2009-07-31 20:28:14 +0000 | [diff] [blame] | 1385 | Constant *NullPtr = Constant::getNullValue(AligningTy->getPointerTo()); |
Owen Anderson | 55f1c09 | 2009-08-13 21:58:54 +0000 | [diff] [blame] | 1386 | Constant *Zero = ConstantInt::get(Type::getInt32Ty(Ty->getContext()), 0); |
| 1387 | Constant *One = ConstantInt::get(Type::getInt32Ty(Ty->getContext()), 1); |
Owen Anderson | 487375e | 2009-07-29 18:55:55 +0000 | [diff] [blame] | 1388 | Constant *Indices[2] = { Zero, One }; |
| 1389 | Constant *GEP = getGetElementPtr(NullPtr, Indices, 2); |
Owen Anderson | 55f1c09 | 2009-08-13 21:58:54 +0000 | [diff] [blame] | 1390 | return getCast(Instruction::PtrToInt, GEP, |
| 1391 | Type::getInt32Ty(Ty->getContext())); |
Owen Anderson | 487375e | 2009-07-29 18:55:55 +0000 | [diff] [blame] | 1392 | } |
| 1393 | |
Dan Gohman | ff3af725 | 2009-08-16 21:26:11 +0000 | [diff] [blame^] | 1394 | Constant* ConstantExpr::getOffsetOf(const StructType* STy, unsigned FieldNo) { |
| 1395 | // offsetof is implemented as: (i64) gep (Ty*)null, 0, FieldNo |
| 1396 | // Note that a non-inbounds gep is used, as null isn't within any object. |
| 1397 | Constant *GEPIdx[] = { |
| 1398 | ConstantInt::get(Type::getInt64Ty(STy->getContext()), 0), |
| 1399 | ConstantInt::get(Type::getInt32Ty(STy->getContext()), FieldNo) |
| 1400 | }; |
| 1401 | Constant *GEP = getGetElementPtr( |
| 1402 | Constant::getNullValue(PointerType::getUnqual(STy)), GEPIdx, 2); |
| 1403 | return getCast(Instruction::PtrToInt, GEP, |
| 1404 | Type::getInt64Ty(STy->getContext())); |
| 1405 | } |
Owen Anderson | 487375e | 2009-07-29 18:55:55 +0000 | [diff] [blame] | 1406 | |
Reid Spencer | 266e42b | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 1407 | Constant *ConstantExpr::getCompare(unsigned short pred, |
Reid Spencer | a009d0d | 2006-12-04 21:35:24 +0000 | [diff] [blame] | 1408 | Constant *C1, Constant *C2) { |
| 1409 | assert(C1->getType() == C2->getType() && "Op types should be identical!"); |
Reid Spencer | 266e42b | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 1410 | return getCompareTy(pred, C1, C2); |
Chris Lattner | 29ca2c6 | 2004-08-04 18:50:09 +0000 | [diff] [blame] | 1411 | } |
| 1412 | |
Chris Lattner | 6e415c0 | 2004-03-12 05:54:04 +0000 | [diff] [blame] | 1413 | Constant *ConstantExpr::getSelectTy(const Type *ReqTy, Constant *C, |
Owen Anderson | 0d2de8c | 2009-06-20 00:24:58 +0000 | [diff] [blame] | 1414 | Constant *V1, Constant *V2) { |
Chris Lattner | 4163213 | 2008-12-29 00:16:12 +0000 | [diff] [blame] | 1415 | assert(!SelectInst::areInvalidOperands(C, V1, V2)&&"Invalid select operands"); |
Chris Lattner | 6e415c0 | 2004-03-12 05:54:04 +0000 | [diff] [blame] | 1416 | |
| 1417 | if (ReqTy == V1->getType()) |
Owen Anderson | 53a5221 | 2009-07-13 04:09:18 +0000 | [diff] [blame] | 1418 | if (Constant *SC = ConstantFoldSelectInstruction( |
Owen Anderson | 23a204d | 2009-07-31 17:39:07 +0000 | [diff] [blame] | 1419 | ReqTy->getContext(), C, V1, V2)) |
Chris Lattner | 6e415c0 | 2004-03-12 05:54:04 +0000 | [diff] [blame] | 1420 | return SC; // Fold common cases |
| 1421 | |
| 1422 | std::vector<Constant*> argVec(3, C); |
| 1423 | argVec[1] = V1; |
| 1424 | argVec[2] = V2; |
Reid Spencer | ee3c991 | 2006-12-04 05:19:50 +0000 | [diff] [blame] | 1425 | ExprMapKeyType Key(Instruction::Select, argVec); |
Owen Anderson | 6179404 | 2009-06-17 20:10:08 +0000 | [diff] [blame] | 1426 | |
Owen Anderson | 1584a29 | 2009-08-04 20:25:11 +0000 | [diff] [blame] | 1427 | LLVMContextImpl *pImpl = ReqTy->getContext().pImpl; |
| 1428 | |
Owen Anderson | 0d2de8c | 2009-06-20 00:24:58 +0000 | [diff] [blame] | 1429 | // Implicitly locked. |
Owen Anderson | 1584a29 | 2009-08-04 20:25:11 +0000 | [diff] [blame] | 1430 | return pImpl->ExprConstants.getOrCreate(ReqTy, Key); |
Chris Lattner | 6e415c0 | 2004-03-12 05:54:04 +0000 | [diff] [blame] | 1431 | } |
| 1432 | |
Chris Lattner | b50d135 | 2003-10-05 00:17:43 +0000 | [diff] [blame] | 1433 | Constant *ConstantExpr::getGetElementPtrTy(const Type *ReqTy, Constant *C, |
Chris Lattner | 302116a | 2007-01-31 04:40:28 +0000 | [diff] [blame] | 1434 | Value* const *Idxs, |
Owen Anderson | 0d2de8c | 2009-06-20 00:24:58 +0000 | [diff] [blame] | 1435 | unsigned NumIdx) { |
Dan Gohman | 12fce77 | 2008-05-15 19:50:34 +0000 | [diff] [blame] | 1436 | assert(GetElementPtrInst::getIndexedType(C->getType(), Idxs, |
| 1437 | Idxs+NumIdx) == |
| 1438 | cast<PointerType>(ReqTy)->getElementType() && |
| 1439 | "GEP indices invalid!"); |
Chris Lattner | 04b60fe | 2004-02-16 20:46:13 +0000 | [diff] [blame] | 1440 | |
Owen Anderson | 53a5221 | 2009-07-13 04:09:18 +0000 | [diff] [blame] | 1441 | if (Constant *FC = ConstantFoldGetElementPtr( |
Owen Anderson | 23a204d | 2009-07-31 17:39:07 +0000 | [diff] [blame] | 1442 | ReqTy->getContext(), C, (Constant**)Idxs, NumIdx)) |
Chris Lattner | acdbe71 | 2003-04-17 19:24:48 +0000 | [diff] [blame] | 1443 | return FC; // Fold a few common cases... |
Chris Lattner | 04b60fe | 2004-02-16 20:46:13 +0000 | [diff] [blame] | 1444 | |
Chris Lattner | b50d135 | 2003-10-05 00:17:43 +0000 | [diff] [blame] | 1445 | assert(isa<PointerType>(C->getType()) && |
Chris Lattner | 98fa07b | 2003-05-23 20:03:32 +0000 | [diff] [blame] | 1446 | "Non-pointer type for constant GetElementPtr expression"); |
Vikram S. Adve | 4c48533 | 2002-07-15 18:19:33 +0000 | [diff] [blame] | 1447 | // Look up the constant in the table first to ensure uniqueness |
Chris Lattner | 13128ab | 2004-10-11 22:52:25 +0000 | [diff] [blame] | 1448 | std::vector<Constant*> ArgVec; |
Chris Lattner | 302116a | 2007-01-31 04:40:28 +0000 | [diff] [blame] | 1449 | ArgVec.reserve(NumIdx+1); |
Chris Lattner | 13128ab | 2004-10-11 22:52:25 +0000 | [diff] [blame] | 1450 | ArgVec.push_back(C); |
Chris Lattner | 302116a | 2007-01-31 04:40:28 +0000 | [diff] [blame] | 1451 | for (unsigned i = 0; i != NumIdx; ++i) |
| 1452 | ArgVec.push_back(cast<Constant>(Idxs[i])); |
| 1453 | const ExprMapKeyType Key(Instruction::GetElementPtr, ArgVec); |
Owen Anderson | 6179404 | 2009-06-17 20:10:08 +0000 | [diff] [blame] | 1454 | |
Owen Anderson | 1584a29 | 2009-08-04 20:25:11 +0000 | [diff] [blame] | 1455 | LLVMContextImpl *pImpl = ReqTy->getContext().pImpl; |
| 1456 | |
Owen Anderson | 6179404 | 2009-06-17 20:10:08 +0000 | [diff] [blame] | 1457 | // Implicitly locked. |
Owen Anderson | 1584a29 | 2009-08-04 20:25:11 +0000 | [diff] [blame] | 1458 | return pImpl->ExprConstants.getOrCreate(ReqTy, Key); |
Vikram S. Adve | 4c48533 | 2002-07-15 18:19:33 +0000 | [diff] [blame] | 1459 | } |
| 1460 | |
Chris Lattner | 302116a | 2007-01-31 04:40:28 +0000 | [diff] [blame] | 1461 | Constant *ConstantExpr::getGetElementPtr(Constant *C, Value* const *Idxs, |
Owen Anderson | 0d2de8c | 2009-06-20 00:24:58 +0000 | [diff] [blame] | 1462 | unsigned NumIdx) { |
Chris Lattner | b50d135 | 2003-10-05 00:17:43 +0000 | [diff] [blame] | 1463 | // Get the result type of the getelementptr! |
Chris Lattner | 302116a | 2007-01-31 04:40:28 +0000 | [diff] [blame] | 1464 | const Type *Ty = |
Dan Gohman | 12fce77 | 2008-05-15 19:50:34 +0000 | [diff] [blame] | 1465 | GetElementPtrInst::getIndexedType(C->getType(), Idxs, Idxs+NumIdx); |
Chris Lattner | b50d135 | 2003-10-05 00:17:43 +0000 | [diff] [blame] | 1466 | assert(Ty && "GEP indices invalid!"); |
Christopher Lamb | 54dd24c | 2007-12-11 08:59:05 +0000 | [diff] [blame] | 1467 | unsigned As = cast<PointerType>(C->getType())->getAddressSpace(); |
Owen Anderson | 0d2de8c | 2009-06-20 00:24:58 +0000 | [diff] [blame] | 1468 | return getGetElementPtrTy(PointerType::get(Ty, As), C, Idxs, NumIdx); |
Chris Lattner | 13128ab | 2004-10-11 22:52:25 +0000 | [diff] [blame] | 1469 | } |
| 1470 | |
Dan Gohman | 50c09d0 | 2009-08-11 17:57:01 +0000 | [diff] [blame] | 1471 | Constant *ConstantExpr::getInBoundsGetElementPtr(Constant *C, |
| 1472 | Value* const *Idxs, |
| 1473 | unsigned NumIdx) { |
Daniel Dunbar | 919fe2b | 2009-08-11 18:28:09 +0000 | [diff] [blame] | 1474 | Constant *Result = getGetElementPtr(C, Idxs, NumIdx); |
| 1475 | // Set in bounds attribute, assuming constant folding didn't eliminate the |
| 1476 | // GEP. |
| 1477 | if (GEPOperator *GEP = dyn_cast<GEPOperator>(Result)) |
| 1478 | GEP->setIsInBounds(true); |
Dan Gohman | 50c09d0 | 2009-08-11 17:57:01 +0000 | [diff] [blame] | 1479 | return Result; |
| 1480 | } |
| 1481 | |
Chris Lattner | 302116a | 2007-01-31 04:40:28 +0000 | [diff] [blame] | 1482 | Constant *ConstantExpr::getGetElementPtr(Constant *C, Constant* const *Idxs, |
Owen Anderson | 0d2de8c | 2009-06-20 00:24:58 +0000 | [diff] [blame] | 1483 | unsigned NumIdx) { |
| 1484 | return getGetElementPtr(C, (Value* const *)Idxs, NumIdx); |
Chris Lattner | b50d135 | 2003-10-05 00:17:43 +0000 | [diff] [blame] | 1485 | } |
| 1486 | |
Dan Gohman | 50c09d0 | 2009-08-11 17:57:01 +0000 | [diff] [blame] | 1487 | Constant *ConstantExpr::getInBoundsGetElementPtr(Constant *C, |
| 1488 | Constant* const *Idxs, |
| 1489 | unsigned NumIdx) { |
| 1490 | return getInBoundsGetElementPtr(C, (Value* const *)Idxs, NumIdx); |
| 1491 | } |
Chris Lattner | 302116a | 2007-01-31 04:40:28 +0000 | [diff] [blame] | 1492 | |
Reid Spencer | ee3c991 | 2006-12-04 05:19:50 +0000 | [diff] [blame] | 1493 | Constant * |
| 1494 | ConstantExpr::getICmp(unsigned short pred, Constant* LHS, Constant* RHS) { |
| 1495 | assert(LHS->getType() == RHS->getType()); |
| 1496 | assert(pred >= ICmpInst::FIRST_ICMP_PREDICATE && |
| 1497 | pred <= ICmpInst::LAST_ICMP_PREDICATE && "Invalid ICmp Predicate"); |
| 1498 | |
Owen Anderson | 53a5221 | 2009-07-13 04:09:18 +0000 | [diff] [blame] | 1499 | if (Constant *FC = ConstantFoldCompareInstruction( |
Owen Anderson | 23a204d | 2009-07-31 17:39:07 +0000 | [diff] [blame] | 1500 | LHS->getContext(), pred, LHS, RHS)) |
Reid Spencer | ee3c991 | 2006-12-04 05:19:50 +0000 | [diff] [blame] | 1501 | return FC; // Fold a few common cases... |
| 1502 | |
| 1503 | // Look up the constant in the table first to ensure uniqueness |
| 1504 | std::vector<Constant*> ArgVec; |
| 1505 | ArgVec.push_back(LHS); |
| 1506 | ArgVec.push_back(RHS); |
Reid Spencer | b153749 | 2006-12-24 18:42:29 +0000 | [diff] [blame] | 1507 | // Get the key type with both the opcode and predicate |
Reid Spencer | ee3c991 | 2006-12-04 05:19:50 +0000 | [diff] [blame] | 1508 | const ExprMapKeyType Key(Instruction::ICmp, ArgVec, pred); |
Owen Anderson | 6179404 | 2009-06-17 20:10:08 +0000 | [diff] [blame] | 1509 | |
Owen Anderson | 1584a29 | 2009-08-04 20:25:11 +0000 | [diff] [blame] | 1510 | LLVMContextImpl *pImpl = LHS->getType()->getContext().pImpl; |
| 1511 | |
Owen Anderson | 6179404 | 2009-06-17 20:10:08 +0000 | [diff] [blame] | 1512 | // Implicitly locked. |
Owen Anderson | 55f1c09 | 2009-08-13 21:58:54 +0000 | [diff] [blame] | 1513 | return |
| 1514 | pImpl->ExprConstants.getOrCreate(Type::getInt1Ty(LHS->getContext()), Key); |
Reid Spencer | ee3c991 | 2006-12-04 05:19:50 +0000 | [diff] [blame] | 1515 | } |
| 1516 | |
| 1517 | Constant * |
| 1518 | ConstantExpr::getFCmp(unsigned short pred, Constant* LHS, Constant* RHS) { |
| 1519 | assert(LHS->getType() == RHS->getType()); |
| 1520 | assert(pred <= FCmpInst::LAST_FCMP_PREDICATE && "Invalid FCmp Predicate"); |
| 1521 | |
Owen Anderson | 53a5221 | 2009-07-13 04:09:18 +0000 | [diff] [blame] | 1522 | if (Constant *FC = ConstantFoldCompareInstruction( |
Owen Anderson | 23a204d | 2009-07-31 17:39:07 +0000 | [diff] [blame] | 1523 | LHS->getContext(), pred, LHS, RHS)) |
Reid Spencer | ee3c991 | 2006-12-04 05:19:50 +0000 | [diff] [blame] | 1524 | return FC; // Fold a few common cases... |
| 1525 | |
| 1526 | // Look up the constant in the table first to ensure uniqueness |
| 1527 | std::vector<Constant*> ArgVec; |
| 1528 | ArgVec.push_back(LHS); |
| 1529 | ArgVec.push_back(RHS); |
Reid Spencer | b153749 | 2006-12-24 18:42:29 +0000 | [diff] [blame] | 1530 | // Get the key type with both the opcode and predicate |
Reid Spencer | ee3c991 | 2006-12-04 05:19:50 +0000 | [diff] [blame] | 1531 | const ExprMapKeyType Key(Instruction::FCmp, ArgVec, pred); |
Owen Anderson | 6179404 | 2009-06-17 20:10:08 +0000 | [diff] [blame] | 1532 | |
Owen Anderson | 1584a29 | 2009-08-04 20:25:11 +0000 | [diff] [blame] | 1533 | LLVMContextImpl *pImpl = LHS->getType()->getContext().pImpl; |
| 1534 | |
Owen Anderson | 6179404 | 2009-06-17 20:10:08 +0000 | [diff] [blame] | 1535 | // Implicitly locked. |
Owen Anderson | 55f1c09 | 2009-08-13 21:58:54 +0000 | [diff] [blame] | 1536 | return |
| 1537 | pImpl->ExprConstants.getOrCreate(Type::getInt1Ty(LHS->getContext()), Key); |
Reid Spencer | ee3c991 | 2006-12-04 05:19:50 +0000 | [diff] [blame] | 1538 | } |
| 1539 | |
Robert Bocchino | 2300448 | 2006-01-10 19:05:34 +0000 | [diff] [blame] | 1540 | Constant *ConstantExpr::getExtractElementTy(const Type *ReqTy, Constant *Val, |
| 1541 | Constant *Idx) { |
Owen Anderson | 53a5221 | 2009-07-13 04:09:18 +0000 | [diff] [blame] | 1542 | if (Constant *FC = ConstantFoldExtractElementInstruction( |
Owen Anderson | 23a204d | 2009-07-31 17:39:07 +0000 | [diff] [blame] | 1543 | ReqTy->getContext(), Val, Idx)) |
Robert Bocchino | de7f1c9 | 2006-01-10 20:03:46 +0000 | [diff] [blame] | 1544 | return FC; // Fold a few common cases... |
Robert Bocchino | 2300448 | 2006-01-10 19:05:34 +0000 | [diff] [blame] | 1545 | // Look up the constant in the table first to ensure uniqueness |
| 1546 | std::vector<Constant*> ArgVec(1, Val); |
| 1547 | ArgVec.push_back(Idx); |
Reid Spencer | ee3c991 | 2006-12-04 05:19:50 +0000 | [diff] [blame] | 1548 | const ExprMapKeyType Key(Instruction::ExtractElement,ArgVec); |
Owen Anderson | 6179404 | 2009-06-17 20:10:08 +0000 | [diff] [blame] | 1549 | |
Owen Anderson | 1584a29 | 2009-08-04 20:25:11 +0000 | [diff] [blame] | 1550 | LLVMContextImpl *pImpl = ReqTy->getContext().pImpl; |
| 1551 | |
Owen Anderson | 6179404 | 2009-06-17 20:10:08 +0000 | [diff] [blame] | 1552 | // Implicitly locked. |
Owen Anderson | 1584a29 | 2009-08-04 20:25:11 +0000 | [diff] [blame] | 1553 | return pImpl->ExprConstants.getOrCreate(ReqTy, Key); |
Robert Bocchino | 2300448 | 2006-01-10 19:05:34 +0000 | [diff] [blame] | 1554 | } |
| 1555 | |
| 1556 | Constant *ConstantExpr::getExtractElement(Constant *Val, Constant *Idx) { |
Reid Spencer | d84d35b | 2007-02-15 02:26:10 +0000 | [diff] [blame] | 1557 | assert(isa<VectorType>(Val->getType()) && |
Reid Spencer | 09575ba | 2007-02-15 03:39:18 +0000 | [diff] [blame] | 1558 | "Tried to create extractelement operation on non-vector type!"); |
Owen Anderson | 55f1c09 | 2009-08-13 21:58:54 +0000 | [diff] [blame] | 1559 | assert(Idx->getType() == Type::getInt32Ty(Val->getContext()) && |
Reid Spencer | 2546b76 | 2007-01-26 07:37:34 +0000 | [diff] [blame] | 1560 | "Extractelement index must be i32 type!"); |
Reid Spencer | d84d35b | 2007-02-15 02:26:10 +0000 | [diff] [blame] | 1561 | return getExtractElementTy(cast<VectorType>(Val->getType())->getElementType(), |
Robert Bocchino | 2300448 | 2006-01-10 19:05:34 +0000 | [diff] [blame] | 1562 | Val, Idx); |
| 1563 | } |
Chris Lattner | b50d135 | 2003-10-05 00:17:43 +0000 | [diff] [blame] | 1564 | |
Robert Bocchino | ca27f03 | 2006-01-17 20:07:22 +0000 | [diff] [blame] | 1565 | Constant *ConstantExpr::getInsertElementTy(const Type *ReqTy, Constant *Val, |
| 1566 | Constant *Elt, Constant *Idx) { |
Owen Anderson | 53a5221 | 2009-07-13 04:09:18 +0000 | [diff] [blame] | 1567 | if (Constant *FC = ConstantFoldInsertElementInstruction( |
Owen Anderson | 23a204d | 2009-07-31 17:39:07 +0000 | [diff] [blame] | 1568 | ReqTy->getContext(), Val, Elt, Idx)) |
Robert Bocchino | ca27f03 | 2006-01-17 20:07:22 +0000 | [diff] [blame] | 1569 | return FC; // Fold a few common cases... |
| 1570 | // Look up the constant in the table first to ensure uniqueness |
| 1571 | std::vector<Constant*> ArgVec(1, Val); |
| 1572 | ArgVec.push_back(Elt); |
| 1573 | ArgVec.push_back(Idx); |
Reid Spencer | ee3c991 | 2006-12-04 05:19:50 +0000 | [diff] [blame] | 1574 | const ExprMapKeyType Key(Instruction::InsertElement,ArgVec); |
Owen Anderson | 6179404 | 2009-06-17 20:10:08 +0000 | [diff] [blame] | 1575 | |
Owen Anderson | 1584a29 | 2009-08-04 20:25:11 +0000 | [diff] [blame] | 1576 | LLVMContextImpl *pImpl = ReqTy->getContext().pImpl; |
| 1577 | |
Owen Anderson | 6179404 | 2009-06-17 20:10:08 +0000 | [diff] [blame] | 1578 | // Implicitly locked. |
Owen Anderson | 1584a29 | 2009-08-04 20:25:11 +0000 | [diff] [blame] | 1579 | return pImpl->ExprConstants.getOrCreate(ReqTy, Key); |
Robert Bocchino | ca27f03 | 2006-01-17 20:07:22 +0000 | [diff] [blame] | 1580 | } |
| 1581 | |
| 1582 | Constant *ConstantExpr::getInsertElement(Constant *Val, Constant *Elt, |
| 1583 | Constant *Idx) { |
Reid Spencer | d84d35b | 2007-02-15 02:26:10 +0000 | [diff] [blame] | 1584 | assert(isa<VectorType>(Val->getType()) && |
Reid Spencer | 09575ba | 2007-02-15 03:39:18 +0000 | [diff] [blame] | 1585 | "Tried to create insertelement operation on non-vector type!"); |
Reid Spencer | d84d35b | 2007-02-15 02:26:10 +0000 | [diff] [blame] | 1586 | assert(Elt->getType() == cast<VectorType>(Val->getType())->getElementType() |
Robert Bocchino | ca27f03 | 2006-01-17 20:07:22 +0000 | [diff] [blame] | 1587 | && "Insertelement types must match!"); |
Owen Anderson | 55f1c09 | 2009-08-13 21:58:54 +0000 | [diff] [blame] | 1588 | assert(Idx->getType() == Type::getInt32Ty(Val->getContext()) && |
Reid Spencer | 2546b76 | 2007-01-26 07:37:34 +0000 | [diff] [blame] | 1589 | "Insertelement index must be i32 type!"); |
Gordon Henriksen | b52d1ed | 2008-08-30 15:41:51 +0000 | [diff] [blame] | 1590 | return getInsertElementTy(Val->getType(), Val, Elt, Idx); |
Robert Bocchino | ca27f03 | 2006-01-17 20:07:22 +0000 | [diff] [blame] | 1591 | } |
| 1592 | |
Chris Lattner | bbe0a42 | 2006-04-08 01:18:18 +0000 | [diff] [blame] | 1593 | Constant *ConstantExpr::getShuffleVectorTy(const Type *ReqTy, Constant *V1, |
| 1594 | Constant *V2, Constant *Mask) { |
Owen Anderson | 53a5221 | 2009-07-13 04:09:18 +0000 | [diff] [blame] | 1595 | if (Constant *FC = ConstantFoldShuffleVectorInstruction( |
Owen Anderson | 23a204d | 2009-07-31 17:39:07 +0000 | [diff] [blame] | 1596 | ReqTy->getContext(), V1, V2, Mask)) |
Chris Lattner | bbe0a42 | 2006-04-08 01:18:18 +0000 | [diff] [blame] | 1597 | return FC; // Fold a few common cases... |
| 1598 | // Look up the constant in the table first to ensure uniqueness |
| 1599 | std::vector<Constant*> ArgVec(1, V1); |
| 1600 | ArgVec.push_back(V2); |
| 1601 | ArgVec.push_back(Mask); |
Reid Spencer | ee3c991 | 2006-12-04 05:19:50 +0000 | [diff] [blame] | 1602 | const ExprMapKeyType Key(Instruction::ShuffleVector,ArgVec); |
Owen Anderson | 6179404 | 2009-06-17 20:10:08 +0000 | [diff] [blame] | 1603 | |
Owen Anderson | 1584a29 | 2009-08-04 20:25:11 +0000 | [diff] [blame] | 1604 | LLVMContextImpl *pImpl = ReqTy->getContext().pImpl; |
| 1605 | |
Owen Anderson | 6179404 | 2009-06-17 20:10:08 +0000 | [diff] [blame] | 1606 | // Implicitly locked. |
Owen Anderson | 1584a29 | 2009-08-04 20:25:11 +0000 | [diff] [blame] | 1607 | return pImpl->ExprConstants.getOrCreate(ReqTy, Key); |
Chris Lattner | bbe0a42 | 2006-04-08 01:18:18 +0000 | [diff] [blame] | 1608 | } |
| 1609 | |
| 1610 | Constant *ConstantExpr::getShuffleVector(Constant *V1, Constant *V2, |
| 1611 | Constant *Mask) { |
| 1612 | assert(ShuffleVectorInst::isValidOperands(V1, V2, Mask) && |
| 1613 | "Invalid shuffle vector constant expr operands!"); |
Nate Begeman | 94aa38d | 2009-02-12 21:28:33 +0000 | [diff] [blame] | 1614 | |
| 1615 | unsigned NElts = cast<VectorType>(Mask->getType())->getNumElements(); |
| 1616 | const Type *EltTy = cast<VectorType>(V1->getType())->getElementType(); |
| 1617 | const Type *ShufTy = VectorType::get(EltTy, NElts); |
| 1618 | return getShuffleVectorTy(ShufTy, V1, V2, Mask); |
Chris Lattner | bbe0a42 | 2006-04-08 01:18:18 +0000 | [diff] [blame] | 1619 | } |
| 1620 | |
Dan Gohman | 12fce77 | 2008-05-15 19:50:34 +0000 | [diff] [blame] | 1621 | Constant *ConstantExpr::getInsertValueTy(const Type *ReqTy, Constant *Agg, |
| 1622 | Constant *Val, |
Dan Gohman | 1ecaf45 | 2008-05-31 00:58:22 +0000 | [diff] [blame] | 1623 | const unsigned *Idxs, unsigned NumIdx) { |
Dan Gohman | 12fce77 | 2008-05-15 19:50:34 +0000 | [diff] [blame] | 1624 | assert(ExtractValueInst::getIndexedType(Agg->getType(), Idxs, |
| 1625 | Idxs+NumIdx) == Val->getType() && |
| 1626 | "insertvalue indices invalid!"); |
| 1627 | assert(Agg->getType() == ReqTy && |
| 1628 | "insertvalue type invalid!"); |
Dan Gohman | 0752bff | 2008-05-23 00:36:11 +0000 | [diff] [blame] | 1629 | assert(Agg->getType()->isFirstClassType() && |
| 1630 | "Non-first-class type for constant InsertValue expression"); |
Owen Anderson | 53a5221 | 2009-07-13 04:09:18 +0000 | [diff] [blame] | 1631 | Constant *FC = ConstantFoldInsertValueInstruction( |
Owen Anderson | 23a204d | 2009-07-31 17:39:07 +0000 | [diff] [blame] | 1632 | ReqTy->getContext(), Agg, Val, Idxs, NumIdx); |
Dan Gohman | d5d24f6 | 2008-07-21 23:30:30 +0000 | [diff] [blame] | 1633 | assert(FC && "InsertValue constant expr couldn't be folded!"); |
| 1634 | return FC; |
Dan Gohman | 12fce77 | 2008-05-15 19:50:34 +0000 | [diff] [blame] | 1635 | } |
| 1636 | |
| 1637 | Constant *ConstantExpr::getInsertValue(Constant *Agg, Constant *Val, |
Dan Gohman | 1ecaf45 | 2008-05-31 00:58:22 +0000 | [diff] [blame] | 1638 | const unsigned *IdxList, unsigned NumIdx) { |
Dan Gohman | 0752bff | 2008-05-23 00:36:11 +0000 | [diff] [blame] | 1639 | assert(Agg->getType()->isFirstClassType() && |
| 1640 | "Tried to create insertelement operation on non-first-class type!"); |
Dan Gohman | 12fce77 | 2008-05-15 19:50:34 +0000 | [diff] [blame] | 1641 | |
Dan Gohman | 0752bff | 2008-05-23 00:36:11 +0000 | [diff] [blame] | 1642 | const Type *ReqTy = Agg->getType(); |
Devang Patel | d26344d | 2008-11-03 23:20:04 +0000 | [diff] [blame] | 1643 | #ifndef NDEBUG |
Dan Gohman | 0752bff | 2008-05-23 00:36:11 +0000 | [diff] [blame] | 1644 | const Type *ValTy = |
Dan Gohman | 12fce77 | 2008-05-15 19:50:34 +0000 | [diff] [blame] | 1645 | ExtractValueInst::getIndexedType(Agg->getType(), IdxList, IdxList+NumIdx); |
Devang Patel | d26344d | 2008-11-03 23:20:04 +0000 | [diff] [blame] | 1646 | #endif |
Dan Gohman | 0752bff | 2008-05-23 00:36:11 +0000 | [diff] [blame] | 1647 | assert(ValTy == Val->getType() && "insertvalue indices invalid!"); |
Dan Gohman | 12fce77 | 2008-05-15 19:50:34 +0000 | [diff] [blame] | 1648 | return getInsertValueTy(ReqTy, Agg, Val, IdxList, NumIdx); |
| 1649 | } |
| 1650 | |
| 1651 | Constant *ConstantExpr::getExtractValueTy(const Type *ReqTy, Constant *Agg, |
Dan Gohman | 1ecaf45 | 2008-05-31 00:58:22 +0000 | [diff] [blame] | 1652 | const unsigned *Idxs, unsigned NumIdx) { |
Dan Gohman | 12fce77 | 2008-05-15 19:50:34 +0000 | [diff] [blame] | 1653 | assert(ExtractValueInst::getIndexedType(Agg->getType(), Idxs, |
| 1654 | Idxs+NumIdx) == ReqTy && |
| 1655 | "extractvalue indices invalid!"); |
Dan Gohman | 0752bff | 2008-05-23 00:36:11 +0000 | [diff] [blame] | 1656 | assert(Agg->getType()->isFirstClassType() && |
| 1657 | "Non-first-class type for constant extractvalue expression"); |
Owen Anderson | 53a5221 | 2009-07-13 04:09:18 +0000 | [diff] [blame] | 1658 | Constant *FC = ConstantFoldExtractValueInstruction( |
Owen Anderson | 23a204d | 2009-07-31 17:39:07 +0000 | [diff] [blame] | 1659 | ReqTy->getContext(), Agg, Idxs, NumIdx); |
Dan Gohman | d5d24f6 | 2008-07-21 23:30:30 +0000 | [diff] [blame] | 1660 | assert(FC && "ExtractValue constant expr couldn't be folded!"); |
| 1661 | return FC; |
Dan Gohman | 12fce77 | 2008-05-15 19:50:34 +0000 | [diff] [blame] | 1662 | } |
| 1663 | |
| 1664 | Constant *ConstantExpr::getExtractValue(Constant *Agg, |
Dan Gohman | 1ecaf45 | 2008-05-31 00:58:22 +0000 | [diff] [blame] | 1665 | const unsigned *IdxList, unsigned NumIdx) { |
Dan Gohman | 0752bff | 2008-05-23 00:36:11 +0000 | [diff] [blame] | 1666 | assert(Agg->getType()->isFirstClassType() && |
| 1667 | "Tried to create extractelement operation on non-first-class type!"); |
Dan Gohman | 12fce77 | 2008-05-15 19:50:34 +0000 | [diff] [blame] | 1668 | |
| 1669 | const Type *ReqTy = |
| 1670 | ExtractValueInst::getIndexedType(Agg->getType(), IdxList, IdxList+NumIdx); |
| 1671 | assert(ReqTy && "extractvalue indices invalid!"); |
| 1672 | return getExtractValueTy(ReqTy, Agg, IdxList, NumIdx); |
| 1673 | } |
| 1674 | |
Owen Anderson | 487375e | 2009-07-29 18:55:55 +0000 | [diff] [blame] | 1675 | Constant* ConstantExpr::getNeg(Constant* C) { |
| 1676 | // API compatibility: Adjust integer opcodes to floating-point opcodes. |
| 1677 | if (C->getType()->isFPOrFPVector()) |
| 1678 | return getFNeg(C); |
| 1679 | assert(C->getType()->isIntOrIntVector() && |
| 1680 | "Cannot NEG a nonintegral value!"); |
| 1681 | return get(Instruction::Sub, |
| 1682 | ConstantFP::getZeroValueForNegation(C->getType()), |
| 1683 | C); |
| 1684 | } |
| 1685 | |
| 1686 | Constant* ConstantExpr::getFNeg(Constant* C) { |
| 1687 | assert(C->getType()->isFPOrFPVector() && |
| 1688 | "Cannot FNEG a non-floating-point value!"); |
| 1689 | return get(Instruction::FSub, |
| 1690 | ConstantFP::getZeroValueForNegation(C->getType()), |
| 1691 | C); |
| 1692 | } |
| 1693 | |
| 1694 | Constant* ConstantExpr::getNot(Constant* C) { |
| 1695 | assert(C->getType()->isIntOrIntVector() && |
| 1696 | "Cannot NOT a nonintegral value!"); |
Owen Anderson | 5a1acd9 | 2009-07-31 20:28:14 +0000 | [diff] [blame] | 1697 | return get(Instruction::Xor, C, Constant::getAllOnesValue(C->getType())); |
Owen Anderson | 487375e | 2009-07-29 18:55:55 +0000 | [diff] [blame] | 1698 | } |
| 1699 | |
| 1700 | Constant* ConstantExpr::getAdd(Constant* C1, Constant* C2) { |
| 1701 | return get(Instruction::Add, C1, C2); |
| 1702 | } |
| 1703 | |
| 1704 | Constant* ConstantExpr::getFAdd(Constant* C1, Constant* C2) { |
| 1705 | return get(Instruction::FAdd, C1, C2); |
| 1706 | } |
| 1707 | |
| 1708 | Constant* ConstantExpr::getSub(Constant* C1, Constant* C2) { |
| 1709 | return get(Instruction::Sub, C1, C2); |
| 1710 | } |
| 1711 | |
| 1712 | Constant* ConstantExpr::getFSub(Constant* C1, Constant* C2) { |
| 1713 | return get(Instruction::FSub, C1, C2); |
| 1714 | } |
| 1715 | |
| 1716 | Constant* ConstantExpr::getMul(Constant* C1, Constant* C2) { |
| 1717 | return get(Instruction::Mul, C1, C2); |
| 1718 | } |
| 1719 | |
| 1720 | Constant* ConstantExpr::getFMul(Constant* C1, Constant* C2) { |
| 1721 | return get(Instruction::FMul, C1, C2); |
| 1722 | } |
| 1723 | |
| 1724 | Constant* ConstantExpr::getUDiv(Constant* C1, Constant* C2) { |
| 1725 | return get(Instruction::UDiv, C1, C2); |
| 1726 | } |
| 1727 | |
| 1728 | Constant* ConstantExpr::getSDiv(Constant* C1, Constant* C2) { |
| 1729 | return get(Instruction::SDiv, C1, C2); |
| 1730 | } |
| 1731 | |
| 1732 | Constant* ConstantExpr::getFDiv(Constant* C1, Constant* C2) { |
| 1733 | return get(Instruction::FDiv, C1, C2); |
| 1734 | } |
| 1735 | |
| 1736 | Constant* ConstantExpr::getURem(Constant* C1, Constant* C2) { |
| 1737 | return get(Instruction::URem, C1, C2); |
| 1738 | } |
| 1739 | |
| 1740 | Constant* ConstantExpr::getSRem(Constant* C1, Constant* C2) { |
| 1741 | return get(Instruction::SRem, C1, C2); |
| 1742 | } |
| 1743 | |
| 1744 | Constant* ConstantExpr::getFRem(Constant* C1, Constant* C2) { |
| 1745 | return get(Instruction::FRem, C1, C2); |
| 1746 | } |
| 1747 | |
| 1748 | Constant* ConstantExpr::getAnd(Constant* C1, Constant* C2) { |
| 1749 | return get(Instruction::And, C1, C2); |
| 1750 | } |
| 1751 | |
| 1752 | Constant* ConstantExpr::getOr(Constant* C1, Constant* C2) { |
| 1753 | return get(Instruction::Or, C1, C2); |
| 1754 | } |
| 1755 | |
| 1756 | Constant* ConstantExpr::getXor(Constant* C1, Constant* C2) { |
| 1757 | return get(Instruction::Xor, C1, C2); |
| 1758 | } |
| 1759 | |
| 1760 | Constant* ConstantExpr::getShl(Constant* C1, Constant* C2) { |
| 1761 | return get(Instruction::Shl, C1, C2); |
| 1762 | } |
| 1763 | |
| 1764 | Constant* ConstantExpr::getLShr(Constant* C1, Constant* C2) { |
| 1765 | return get(Instruction::LShr, C1, C2); |
| 1766 | } |
| 1767 | |
| 1768 | Constant* ConstantExpr::getAShr(Constant* C1, Constant* C2) { |
| 1769 | return get(Instruction::AShr, C1, C2); |
| 1770 | } |
| 1771 | |
Vikram S. Adve | 4c48533 | 2002-07-15 18:19:33 +0000 | [diff] [blame] | 1772 | // destroyConstant - Remove the constant from the constant table... |
| 1773 | // |
Owen Anderson | 0d2de8c | 2009-06-20 00:24:58 +0000 | [diff] [blame] | 1774 | void ConstantExpr::destroyConstant() { |
| 1775 | // Implicitly locked. |
Owen Anderson | 1584a29 | 2009-08-04 20:25:11 +0000 | [diff] [blame] | 1776 | LLVMContextImpl *pImpl = getType()->getContext().pImpl; |
| 1777 | pImpl->ExprConstants.remove(this); |
Vikram S. Adve | 4c48533 | 2002-07-15 18:19:33 +0000 | [diff] [blame] | 1778 | destroyConstantImpl(); |
Vikram S. Adve | 4e537b2 | 2002-07-14 23:13:17 +0000 | [diff] [blame] | 1779 | } |
| 1780 | |
Chris Lattner | 3cd8c56 | 2002-07-30 18:54:25 +0000 | [diff] [blame] | 1781 | const char *ConstantExpr::getOpcodeName() const { |
| 1782 | return Instruction::getOpcodeName(getOpcode()); |
Vikram S. Adve | 4e537b2 | 2002-07-14 23:13:17 +0000 | [diff] [blame] | 1783 | } |
Reid Spencer | 1ebe1ab | 2004-07-17 23:48:33 +0000 | [diff] [blame] | 1784 | |
Chris Lattner | c4062ba | 2005-10-03 21:58:36 +0000 | [diff] [blame] | 1785 | //===----------------------------------------------------------------------===// |
| 1786 | // replaceUsesOfWithOnConstant implementations |
| 1787 | |
Chris Lattner | 913849b | 2007-08-21 00:55:23 +0000 | [diff] [blame] | 1788 | /// replaceUsesOfWithOnConstant - Update this constant array to change uses of |
| 1789 | /// 'From' to be uses of 'To'. This must update the uniquing data structures |
| 1790 | /// etc. |
| 1791 | /// |
| 1792 | /// Note that we intentionally replace all uses of From with To here. Consider |
| 1793 | /// a large array that uses 'From' 1000 times. By handling this case all here, |
| 1794 | /// ConstantArray::replaceUsesOfWithOnConstant is only invoked once, and that |
| 1795 | /// single invocation handles all 1000 uses. Handling them one at a time would |
| 1796 | /// work, but would be really slow because it would have to unique each updated |
| 1797 | /// array instance. |
Owen Anderson | c2c7932 | 2009-07-28 18:32:17 +0000 | [diff] [blame] | 1798 | |
| 1799 | static std::vector<Constant*> getValType(ConstantArray *CA) { |
| 1800 | std::vector<Constant*> Elements; |
| 1801 | Elements.reserve(CA->getNumOperands()); |
| 1802 | for (unsigned i = 0, e = CA->getNumOperands(); i != e; ++i) |
| 1803 | Elements.push_back(cast<Constant>(CA->getOperand(i))); |
| 1804 | return Elements; |
| 1805 | } |
| 1806 | |
| 1807 | |
Chris Lattner | c4062ba | 2005-10-03 21:58:36 +0000 | [diff] [blame] | 1808 | void ConstantArray::replaceUsesOfWithOnConstant(Value *From, Value *To, |
Chris Lattner | 7a1450d | 2005-10-04 18:13:04 +0000 | [diff] [blame] | 1809 | Use *U) { |
Owen Anderson | c2c7932 | 2009-07-28 18:32:17 +0000 | [diff] [blame] | 1810 | assert(isa<Constant>(To) && "Cannot make Constant refer to non-constant!"); |
| 1811 | Constant *ToC = cast<Constant>(To); |
| 1812 | |
| 1813 | LLVMContext &Context = getType()->getContext(); |
| 1814 | LLVMContextImpl *pImpl = Context.pImpl; |
| 1815 | |
| 1816 | std::pair<LLVMContextImpl::ArrayConstantsTy::MapKey, Constant*> Lookup; |
| 1817 | Lookup.first.first = getType(); |
| 1818 | Lookup.second = this; |
| 1819 | |
| 1820 | std::vector<Constant*> &Values = Lookup.first.second; |
| 1821 | Values.reserve(getNumOperands()); // Build replacement array. |
| 1822 | |
| 1823 | // Fill values with the modified operands of the constant array. Also, |
| 1824 | // compute whether this turns into an all-zeros array. |
| 1825 | bool isAllZeros = false; |
| 1826 | unsigned NumUpdated = 0; |
| 1827 | if (!ToC->isNullValue()) { |
| 1828 | for (Use *O = OperandList, *E = OperandList+getNumOperands(); O != E; ++O) { |
| 1829 | Constant *Val = cast<Constant>(O->get()); |
| 1830 | if (Val == From) { |
| 1831 | Val = ToC; |
| 1832 | ++NumUpdated; |
| 1833 | } |
| 1834 | Values.push_back(Val); |
| 1835 | } |
| 1836 | } else { |
| 1837 | isAllZeros = true; |
| 1838 | for (Use *O = OperandList, *E = OperandList+getNumOperands();O != E; ++O) { |
| 1839 | Constant *Val = cast<Constant>(O->get()); |
| 1840 | if (Val == From) { |
| 1841 | Val = ToC; |
| 1842 | ++NumUpdated; |
| 1843 | } |
| 1844 | Values.push_back(Val); |
| 1845 | if (isAllZeros) isAllZeros = Val->isNullValue(); |
| 1846 | } |
| 1847 | } |
| 1848 | |
| 1849 | Constant *Replacement = 0; |
| 1850 | if (isAllZeros) { |
Owen Anderson | b292b8c | 2009-07-30 23:03:37 +0000 | [diff] [blame] | 1851 | Replacement = ConstantAggregateZero::get(getType()); |
Owen Anderson | c2c7932 | 2009-07-28 18:32:17 +0000 | [diff] [blame] | 1852 | } else { |
| 1853 | // Check to see if we have this array type already. |
| 1854 | sys::SmartScopedWriter<true> Writer(pImpl->ConstantsLock); |
| 1855 | bool Exists; |
| 1856 | LLVMContextImpl::ArrayConstantsTy::MapTy::iterator I = |
| 1857 | pImpl->ArrayConstants.InsertOrGetItem(Lookup, Exists); |
| 1858 | |
| 1859 | if (Exists) { |
Owen Anderson | 13234f8 | 2009-08-10 18:16:08 +0000 | [diff] [blame] | 1860 | Replacement = cast<Constant>(I->second); |
Owen Anderson | c2c7932 | 2009-07-28 18:32:17 +0000 | [diff] [blame] | 1861 | } else { |
| 1862 | // Okay, the new shape doesn't exist in the system yet. Instead of |
| 1863 | // creating a new constant array, inserting it, replaceallusesof'ing the |
| 1864 | // old with the new, then deleting the old... just update the current one |
| 1865 | // in place! |
| 1866 | pImpl->ArrayConstants.MoveConstantToNewSlot(this, I); |
| 1867 | |
| 1868 | // Update to the new value. Optimize for the case when we have a single |
| 1869 | // operand that we're changing, but handle bulk updates efficiently. |
| 1870 | if (NumUpdated == 1) { |
| 1871 | unsigned OperandToUpdate = U - OperandList; |
| 1872 | assert(getOperand(OperandToUpdate) == From && |
| 1873 | "ReplaceAllUsesWith broken!"); |
| 1874 | setOperand(OperandToUpdate, ToC); |
| 1875 | } else { |
| 1876 | for (unsigned i = 0, e = getNumOperands(); i != e; ++i) |
| 1877 | if (getOperand(i) == From) |
| 1878 | setOperand(i, ToC); |
| 1879 | } |
| 1880 | return; |
| 1881 | } |
| 1882 | } |
Chris Lattner | b64419a | 2005-10-03 22:51:37 +0000 | [diff] [blame] | 1883 | |
| 1884 | // Otherwise, I do need to replace this with an existing value. |
Chris Lattner | c4062ba | 2005-10-03 21:58:36 +0000 | [diff] [blame] | 1885 | assert(Replacement != this && "I didn't contain From!"); |
| 1886 | |
Chris Lattner | 7a1450d | 2005-10-04 18:13:04 +0000 | [diff] [blame] | 1887 | // Everyone using this now uses the replacement. |
| 1888 | uncheckedReplaceAllUsesWith(Replacement); |
Chris Lattner | c4062ba | 2005-10-03 21:58:36 +0000 | [diff] [blame] | 1889 | |
| 1890 | // Delete the old constant! |
| 1891 | destroyConstant(); |
| 1892 | } |
| 1893 | |
Owen Anderson | 45308b5 | 2009-07-27 22:29:26 +0000 | [diff] [blame] | 1894 | static std::vector<Constant*> getValType(ConstantStruct *CS) { |
| 1895 | std::vector<Constant*> Elements; |
| 1896 | Elements.reserve(CS->getNumOperands()); |
| 1897 | for (unsigned i = 0, e = CS->getNumOperands(); i != e; ++i) |
| 1898 | Elements.push_back(cast<Constant>(CS->getOperand(i))); |
| 1899 | return Elements; |
| 1900 | } |
| 1901 | |
Chris Lattner | c4062ba | 2005-10-03 21:58:36 +0000 | [diff] [blame] | 1902 | void ConstantStruct::replaceUsesOfWithOnConstant(Value *From, Value *To, |
Chris Lattner | 7a1450d | 2005-10-04 18:13:04 +0000 | [diff] [blame] | 1903 | Use *U) { |
Owen Anderson | 45308b5 | 2009-07-27 22:29:26 +0000 | [diff] [blame] | 1904 | assert(isa<Constant>(To) && "Cannot make Constant refer to non-constant!"); |
| 1905 | Constant *ToC = cast<Constant>(To); |
| 1906 | |
| 1907 | unsigned OperandToUpdate = U-OperandList; |
| 1908 | assert(getOperand(OperandToUpdate) == From && "ReplaceAllUsesWith broken!"); |
| 1909 | |
| 1910 | std::pair<LLVMContextImpl::StructConstantsTy::MapKey, Constant*> Lookup; |
| 1911 | Lookup.first.first = getType(); |
| 1912 | Lookup.second = this; |
| 1913 | std::vector<Constant*> &Values = Lookup.first.second; |
| 1914 | Values.reserve(getNumOperands()); // Build replacement struct. |
| 1915 | |
| 1916 | |
| 1917 | // Fill values with the modified operands of the constant struct. Also, |
| 1918 | // compute whether this turns into an all-zeros struct. |
| 1919 | bool isAllZeros = false; |
| 1920 | if (!ToC->isNullValue()) { |
| 1921 | for (Use *O = OperandList, *E = OperandList + getNumOperands(); O != E; ++O) |
| 1922 | Values.push_back(cast<Constant>(O->get())); |
| 1923 | } else { |
| 1924 | isAllZeros = true; |
| 1925 | for (Use *O = OperandList, *E = OperandList+getNumOperands(); O != E; ++O) { |
| 1926 | Constant *Val = cast<Constant>(O->get()); |
| 1927 | Values.push_back(Val); |
| 1928 | if (isAllZeros) isAllZeros = Val->isNullValue(); |
| 1929 | } |
| 1930 | } |
| 1931 | Values[OperandToUpdate] = ToC; |
| 1932 | |
| 1933 | LLVMContext &Context = getType()->getContext(); |
| 1934 | LLVMContextImpl *pImpl = Context.pImpl; |
| 1935 | |
| 1936 | Constant *Replacement = 0; |
| 1937 | if (isAllZeros) { |
Owen Anderson | b292b8c | 2009-07-30 23:03:37 +0000 | [diff] [blame] | 1938 | Replacement = ConstantAggregateZero::get(getType()); |
Owen Anderson | 45308b5 | 2009-07-27 22:29:26 +0000 | [diff] [blame] | 1939 | } else { |
| 1940 | // Check to see if we have this array type already. |
| 1941 | sys::SmartScopedWriter<true> Writer(pImpl->ConstantsLock); |
| 1942 | bool Exists; |
| 1943 | LLVMContextImpl::StructConstantsTy::MapTy::iterator I = |
| 1944 | pImpl->StructConstants.InsertOrGetItem(Lookup, Exists); |
| 1945 | |
| 1946 | if (Exists) { |
Owen Anderson | 13234f8 | 2009-08-10 18:16:08 +0000 | [diff] [blame] | 1947 | Replacement = cast<Constant>(I->second); |
Owen Anderson | 45308b5 | 2009-07-27 22:29:26 +0000 | [diff] [blame] | 1948 | } else { |
| 1949 | // Okay, the new shape doesn't exist in the system yet. Instead of |
| 1950 | // creating a new constant struct, inserting it, replaceallusesof'ing the |
| 1951 | // old with the new, then deleting the old... just update the current one |
| 1952 | // in place! |
| 1953 | pImpl->StructConstants.MoveConstantToNewSlot(this, I); |
| 1954 | |
| 1955 | // Update to the new value. |
| 1956 | setOperand(OperandToUpdate, ToC); |
| 1957 | return; |
| 1958 | } |
| 1959 | } |
| 1960 | |
| 1961 | assert(Replacement != this && "I didn't contain From!"); |
Chris Lattner | c4062ba | 2005-10-03 21:58:36 +0000 | [diff] [blame] | 1962 | |
Chris Lattner | 7a1450d | 2005-10-04 18:13:04 +0000 | [diff] [blame] | 1963 | // Everyone using this now uses the replacement. |
| 1964 | uncheckedReplaceAllUsesWith(Replacement); |
Chris Lattner | c4062ba | 2005-10-03 21:58:36 +0000 | [diff] [blame] | 1965 | |
| 1966 | // Delete the old constant! |
| 1967 | destroyConstant(); |
| 1968 | } |
| 1969 | |
Owen Anderson | 4aa3295 | 2009-07-28 21:19:26 +0000 | [diff] [blame] | 1970 | static std::vector<Constant*> getValType(ConstantVector *CP) { |
| 1971 | std::vector<Constant*> Elements; |
| 1972 | Elements.reserve(CP->getNumOperands()); |
| 1973 | for (unsigned i = 0, e = CP->getNumOperands(); i != e; ++i) |
| 1974 | Elements.push_back(CP->getOperand(i)); |
| 1975 | return Elements; |
| 1976 | } |
| 1977 | |
Reid Spencer | d84d35b | 2007-02-15 02:26:10 +0000 | [diff] [blame] | 1978 | void ConstantVector::replaceUsesOfWithOnConstant(Value *From, Value *To, |
Chris Lattner | 7a1450d | 2005-10-04 18:13:04 +0000 | [diff] [blame] | 1979 | Use *U) { |
Chris Lattner | c4062ba | 2005-10-03 21:58:36 +0000 | [diff] [blame] | 1980 | assert(isa<Constant>(To) && "Cannot make Constant refer to non-constant!"); |
| 1981 | |
| 1982 | std::vector<Constant*> Values; |
| 1983 | Values.reserve(getNumOperands()); // Build replacement array... |
| 1984 | for (unsigned i = 0, e = getNumOperands(); i != e; ++i) { |
| 1985 | Constant *Val = getOperand(i); |
| 1986 | if (Val == From) Val = cast<Constant>(To); |
| 1987 | Values.push_back(Val); |
| 1988 | } |
| 1989 | |
Owen Anderson | 4aa3295 | 2009-07-28 21:19:26 +0000 | [diff] [blame] | 1990 | Constant *Replacement = get(getType(), Values); |
Chris Lattner | c4062ba | 2005-10-03 21:58:36 +0000 | [diff] [blame] | 1991 | assert(Replacement != this && "I didn't contain From!"); |
| 1992 | |
Chris Lattner | 7a1450d | 2005-10-04 18:13:04 +0000 | [diff] [blame] | 1993 | // Everyone using this now uses the replacement. |
| 1994 | uncheckedReplaceAllUsesWith(Replacement); |
Chris Lattner | c4062ba | 2005-10-03 21:58:36 +0000 | [diff] [blame] | 1995 | |
| 1996 | // Delete the old constant! |
| 1997 | destroyConstant(); |
| 1998 | } |
| 1999 | |
| 2000 | void ConstantExpr::replaceUsesOfWithOnConstant(Value *From, Value *ToV, |
Chris Lattner | 7a1450d | 2005-10-04 18:13:04 +0000 | [diff] [blame] | 2001 | Use *U) { |
Chris Lattner | c4062ba | 2005-10-03 21:58:36 +0000 | [diff] [blame] | 2002 | assert(isa<Constant>(ToV) && "Cannot make Constant refer to non-constant!"); |
| 2003 | Constant *To = cast<Constant>(ToV); |
| 2004 | |
| 2005 | Constant *Replacement = 0; |
| 2006 | if (getOpcode() == Instruction::GetElementPtr) { |
Chris Lattner | b5d7030 | 2007-02-19 20:01:23 +0000 | [diff] [blame] | 2007 | SmallVector<Constant*, 8> Indices; |
Chris Lattner | c4062ba | 2005-10-03 21:58:36 +0000 | [diff] [blame] | 2008 | Constant *Pointer = getOperand(0); |
| 2009 | Indices.reserve(getNumOperands()-1); |
| 2010 | if (Pointer == From) Pointer = To; |
| 2011 | |
| 2012 | for (unsigned i = 1, e = getNumOperands(); i != e; ++i) { |
| 2013 | Constant *Val = getOperand(i); |
| 2014 | if (Val == From) Val = To; |
| 2015 | Indices.push_back(Val); |
| 2016 | } |
Chris Lattner | b5d7030 | 2007-02-19 20:01:23 +0000 | [diff] [blame] | 2017 | Replacement = ConstantExpr::getGetElementPtr(Pointer, |
| 2018 | &Indices[0], Indices.size()); |
Dan Gohman | 12fce77 | 2008-05-15 19:50:34 +0000 | [diff] [blame] | 2019 | } else if (getOpcode() == Instruction::ExtractValue) { |
Dan Gohman | 12fce77 | 2008-05-15 19:50:34 +0000 | [diff] [blame] | 2020 | Constant *Agg = getOperand(0); |
Dan Gohman | 12fce77 | 2008-05-15 19:50:34 +0000 | [diff] [blame] | 2021 | if (Agg == From) Agg = To; |
| 2022 | |
Dan Gohman | 1ecaf45 | 2008-05-31 00:58:22 +0000 | [diff] [blame] | 2023 | const SmallVector<unsigned, 4> &Indices = getIndices(); |
Dan Gohman | 12fce77 | 2008-05-15 19:50:34 +0000 | [diff] [blame] | 2024 | Replacement = ConstantExpr::getExtractValue(Agg, |
| 2025 | &Indices[0], Indices.size()); |
| 2026 | } else if (getOpcode() == Instruction::InsertValue) { |
Dan Gohman | 12fce77 | 2008-05-15 19:50:34 +0000 | [diff] [blame] | 2027 | Constant *Agg = getOperand(0); |
| 2028 | Constant *Val = getOperand(1); |
Dan Gohman | 12fce77 | 2008-05-15 19:50:34 +0000 | [diff] [blame] | 2029 | if (Agg == From) Agg = To; |
| 2030 | if (Val == From) Val = To; |
| 2031 | |
Dan Gohman | 1ecaf45 | 2008-05-31 00:58:22 +0000 | [diff] [blame] | 2032 | const SmallVector<unsigned, 4> &Indices = getIndices(); |
Dan Gohman | 12fce77 | 2008-05-15 19:50:34 +0000 | [diff] [blame] | 2033 | Replacement = ConstantExpr::getInsertValue(Agg, Val, |
| 2034 | &Indices[0], Indices.size()); |
Reid Spencer | 6c38f0b | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 2035 | } else if (isCast()) { |
Chris Lattner | c4062ba | 2005-10-03 21:58:36 +0000 | [diff] [blame] | 2036 | assert(getOperand(0) == From && "Cast only has one use!"); |
Reid Spencer | 6c38f0b | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 2037 | Replacement = ConstantExpr::getCast(getOpcode(), To, getType()); |
Chris Lattner | c4062ba | 2005-10-03 21:58:36 +0000 | [diff] [blame] | 2038 | } else if (getOpcode() == Instruction::Select) { |
| 2039 | Constant *C1 = getOperand(0); |
| 2040 | Constant *C2 = getOperand(1); |
| 2041 | Constant *C3 = getOperand(2); |
| 2042 | if (C1 == From) C1 = To; |
| 2043 | if (C2 == From) C2 = To; |
| 2044 | if (C3 == From) C3 = To; |
| 2045 | Replacement = ConstantExpr::getSelect(C1, C2, C3); |
Robert Bocchino | 2300448 | 2006-01-10 19:05:34 +0000 | [diff] [blame] | 2046 | } else if (getOpcode() == Instruction::ExtractElement) { |
| 2047 | Constant *C1 = getOperand(0); |
| 2048 | Constant *C2 = getOperand(1); |
| 2049 | if (C1 == From) C1 = To; |
| 2050 | if (C2 == From) C2 = To; |
| 2051 | Replacement = ConstantExpr::getExtractElement(C1, C2); |
Chris Lattner | a93b4b5 | 2006-04-08 05:09:48 +0000 | [diff] [blame] | 2052 | } else if (getOpcode() == Instruction::InsertElement) { |
| 2053 | Constant *C1 = getOperand(0); |
| 2054 | Constant *C2 = getOperand(1); |
| 2055 | Constant *C3 = getOperand(1); |
| 2056 | if (C1 == From) C1 = To; |
| 2057 | if (C2 == From) C2 = To; |
| 2058 | if (C3 == From) C3 = To; |
| 2059 | Replacement = ConstantExpr::getInsertElement(C1, C2, C3); |
| 2060 | } else if (getOpcode() == Instruction::ShuffleVector) { |
| 2061 | Constant *C1 = getOperand(0); |
| 2062 | Constant *C2 = getOperand(1); |
| 2063 | Constant *C3 = getOperand(2); |
| 2064 | if (C1 == From) C1 = To; |
| 2065 | if (C2 == From) C2 = To; |
| 2066 | if (C3 == From) C3 = To; |
| 2067 | Replacement = ConstantExpr::getShuffleVector(C1, C2, C3); |
Reid Spencer | ee3c991 | 2006-12-04 05:19:50 +0000 | [diff] [blame] | 2068 | } else if (isCompare()) { |
| 2069 | Constant *C1 = getOperand(0); |
| 2070 | Constant *C2 = getOperand(1); |
| 2071 | if (C1 == From) C1 = To; |
| 2072 | if (C2 == From) C2 = To; |
| 2073 | if (getOpcode() == Instruction::ICmp) |
| 2074 | Replacement = ConstantExpr::getICmp(getPredicate(), C1, C2); |
Chris Lattner | eab4926 | 2008-07-14 05:17:31 +0000 | [diff] [blame] | 2075 | else { |
Nick Lewycky | a21d3da | 2009-07-08 03:04:38 +0000 | [diff] [blame] | 2076 | assert(getOpcode() == Instruction::FCmp); |
| 2077 | Replacement = ConstantExpr::getFCmp(getPredicate(), C1, C2); |
Chris Lattner | eab4926 | 2008-07-14 05:17:31 +0000 | [diff] [blame] | 2078 | } |
Chris Lattner | c4062ba | 2005-10-03 21:58:36 +0000 | [diff] [blame] | 2079 | } else if (getNumOperands() == 2) { |
| 2080 | Constant *C1 = getOperand(0); |
| 2081 | Constant *C2 = getOperand(1); |
| 2082 | if (C1 == From) C1 = To; |
| 2083 | if (C2 == From) C2 = To; |
| 2084 | Replacement = ConstantExpr::get(getOpcode(), C1, C2); |
| 2085 | } else { |
Torok Edwin | fbcc663 | 2009-07-14 16:55:14 +0000 | [diff] [blame] | 2086 | llvm_unreachable("Unknown ConstantExpr type!"); |
Chris Lattner | c4062ba | 2005-10-03 21:58:36 +0000 | [diff] [blame] | 2087 | return; |
| 2088 | } |
| 2089 | |
| 2090 | assert(Replacement != this && "I didn't contain From!"); |
| 2091 | |
Chris Lattner | 7a1450d | 2005-10-04 18:13:04 +0000 | [diff] [blame] | 2092 | // Everyone using this now uses the replacement. |
| 2093 | uncheckedReplaceAllUsesWith(Replacement); |
Chris Lattner | c4062ba | 2005-10-03 21:58:36 +0000 | [diff] [blame] | 2094 | |
| 2095 | // Delete the old constant! |
| 2096 | destroyConstant(); |
Matthijs Kooijman | ba5d7ef | 2008-07-03 07:46:41 +0000 | [diff] [blame] | 2097 | } |
Nick Lewycky | 49f8919 | 2009-04-04 07:22:01 +0000 | [diff] [blame] | 2098 | |