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