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 | |
Chris Lattner | ca14237 | 2002-04-28 19:55:58 +0000 | [diff] [blame] | 14 | #include "llvm/Constants.h" |
Chris Lattner | 33e93b8 | 2007-02-27 03:05:06 +0000 | [diff] [blame] | 15 | #include "ConstantFold.h" |
Chris Lattner | 2f7c963 | 2001-06-06 20:29:01 +0000 | [diff] [blame] | 16 | #include "llvm/DerivedTypes.h" |
Reid Spencer | 1ebe1ab | 2004-07-17 23:48:33 +0000 | [diff] [blame] | 17 | #include "llvm/GlobalValue.h" |
Misha Brukman | 63b38bd | 2004-07-29 17:30:56 +0000 | [diff] [blame] | 18 | #include "llvm/Instructions.h" |
Nick Lewycky | b8f9b7a | 2009-05-10 20:57:05 +0000 | [diff] [blame] | 19 | #include "llvm/MDNode.h" |
Chris Lattner | d7a7330 | 2001-10-13 06:57:33 +0000 | [diff] [blame] | 20 | #include "llvm/Module.h" |
Nick Lewycky | 49f8919 | 2009-04-04 07:22:01 +0000 | [diff] [blame] | 21 | #include "llvm/ADT/FoldingSet.h" |
Reid Spencer | 7c16caa | 2004-09-01 22:55:40 +0000 | [diff] [blame] | 22 | #include "llvm/ADT/StringExtras.h" |
Nick Lewycky | 49f8919 | 2009-04-04 07:22:01 +0000 | [diff] [blame] | 23 | #include "llvm/ADT/StringMap.h" |
Chris Lattner | 3d27be1 | 2006-08-27 12:54:02 +0000 | [diff] [blame] | 24 | #include "llvm/Support/Compiler.h" |
Bill Wendling | 6a462f1 | 2006-11-17 08:03:48 +0000 | [diff] [blame] | 25 | #include "llvm/Support/Debug.h" |
Torok Edwin | ccb29cd | 2009-07-11 13:10:19 +0000 | [diff] [blame] | 26 | #include "llvm/Support/ErrorHandling.h" |
Chris Lattner | 69edc98 | 2006-09-28 00:35:06 +0000 | [diff] [blame] | 27 | #include "llvm/Support/ManagedStatic.h" |
Bill Wendling | 6a462f1 | 2006-11-17 08:03:48 +0000 | [diff] [blame] | 28 | #include "llvm/Support/MathExtras.h" |
Owen Anderson | 0d2de8c | 2009-06-20 00:24:58 +0000 | [diff] [blame] | 29 | #include "llvm/System/Mutex.h" |
Owen Anderson | 2d7231d | 2009-06-17 18:40:29 +0000 | [diff] [blame] | 30 | #include "llvm/System/RWMutex.h" |
Owen Anderson | 7d42b95 | 2009-06-18 16:54:52 +0000 | [diff] [blame] | 31 | #include "llvm/System/Threading.h" |
Chris Lattner | a80bf0b | 2007-02-20 06:39:57 +0000 | [diff] [blame] | 32 | #include "llvm/ADT/DenseMap.h" |
Chris Lattner | b5d7030 | 2007-02-19 20:01:23 +0000 | [diff] [blame] | 33 | #include "llvm/ADT/SmallVector.h" |
Chris Lattner | 2f7c963 | 2001-06-06 20:29:01 +0000 | [diff] [blame] | 34 | #include <algorithm> |
Reid Spencer | 3aaaa0b | 2007-02-05 20:47:22 +0000 | [diff] [blame] | 35 | #include <map> |
Chris Lattner | 189d19f | 2003-11-21 20:23:48 +0000 | [diff] [blame] | 36 | using namespace llvm; |
Brian Gaeke | 960707c | 2003-11-11 22:41:34 +0000 | [diff] [blame] | 37 | |
Chris Lattner | 2f7c963 | 2001-06-06 20:29:01 +0000 | [diff] [blame] | 38 | //===----------------------------------------------------------------------===// |
Chris Lattner | 3462ae3 | 2001-12-03 22:26:30 +0000 | [diff] [blame] | 39 | // Constant Class |
Chris Lattner | 2f7c963 | 2001-06-06 20:29:01 +0000 | [diff] [blame] | 40 | //===----------------------------------------------------------------------===// |
| 41 | |
Owen Anderson | d830eb8 | 2009-06-18 19:10:19 +0000 | [diff] [blame] | 42 | // Becomes a no-op when multithreading is disabled. |
| 43 | ManagedStatic<sys::SmartRWMutex<true> > ConstantsLock; |
Owen Anderson | 2d7231d | 2009-06-17 18:40:29 +0000 | [diff] [blame] | 44 | |
Chris Lattner | 3462ae3 | 2001-12-03 22:26:30 +0000 | [diff] [blame] | 45 | void Constant::destroyConstantImpl() { |
| 46 | // When a Constant is destroyed, there may be lingering |
Chris Lattner | d7a7330 | 2001-10-13 06:57:33 +0000 | [diff] [blame] | 47 | // references to the constant by other constants in the constant pool. These |
Misha Brukman | be372b9 | 2003-08-21 22:14:26 +0000 | [diff] [blame] | 48 | // constants are implicitly dependent on the module that is being deleted, |
Chris Lattner | d7a7330 | 2001-10-13 06:57:33 +0000 | [diff] [blame] | 49 | // but they don't know that. Because we only find out when the CPV is |
| 50 | // 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] | 51 | // Constants) that they are, in fact, invalid now and should be deleted. |
Chris Lattner | d7a7330 | 2001-10-13 06:57:33 +0000 | [diff] [blame] | 52 | // |
| 53 | while (!use_empty()) { |
| 54 | Value *V = use_back(); |
| 55 | #ifndef NDEBUG // Only in -g mode... |
Chris Lattner | d9f4ac66 | 2002-07-18 00:14:50 +0000 | [diff] [blame] | 56 | if (!isa<Constant>(V)) |
Bill Wendling | 6a462f1 | 2006-11-17 08:03:48 +0000 | [diff] [blame] | 57 | DOUT << "While deleting: " << *this |
| 58 | << "\n\nUse still stuck around after Def is destroyed: " |
| 59 | << *V << "\n\n"; |
Chris Lattner | d7a7330 | 2001-10-13 06:57:33 +0000 | [diff] [blame] | 60 | #endif |
Vikram S. Adve | 4e537b2 | 2002-07-14 23:13:17 +0000 | [diff] [blame] | 61 | assert(isa<Constant>(V) && "References remain to Constant being destroyed"); |
Reid Spencer | 1ebe1ab | 2004-07-17 23:48:33 +0000 | [diff] [blame] | 62 | Constant *CV = cast<Constant>(V); |
| 63 | CV->destroyConstant(); |
Chris Lattner | d7a7330 | 2001-10-13 06:57:33 +0000 | [diff] [blame] | 64 | |
| 65 | // The constant should remove itself from our use list... |
Vikram S. Adve | 4e537b2 | 2002-07-14 23:13:17 +0000 | [diff] [blame] | 66 | assert((use_empty() || use_back() != V) && "Constant not removed!"); |
Chris Lattner | d7a7330 | 2001-10-13 06:57:33 +0000 | [diff] [blame] | 67 | } |
| 68 | |
| 69 | // Value has no outstanding references it is safe to delete it now... |
| 70 | delete this; |
Chris Lattner | 3856934 | 2001-10-01 20:11:19 +0000 | [diff] [blame] | 71 | } |
Chris Lattner | 2f7c963 | 2001-06-06 20:29:01 +0000 | [diff] [blame] | 72 | |
Chris Lattner | 23dd1f6 | 2006-10-20 00:27:06 +0000 | [diff] [blame] | 73 | /// canTrap - Return true if evaluation of this constant could trap. This is |
| 74 | /// true for things like constant expressions that could divide by zero. |
| 75 | bool Constant::canTrap() const { |
| 76 | assert(getType()->isFirstClassType() && "Cannot evaluate aggregate vals!"); |
| 77 | // The only thing that could possibly trap are constant exprs. |
| 78 | const ConstantExpr *CE = dyn_cast<ConstantExpr>(this); |
| 79 | if (!CE) return false; |
| 80 | |
| 81 | // ConstantExpr traps if any operands can trap. |
| 82 | for (unsigned i = 0, e = getNumOperands(); i != e; ++i) |
| 83 | if (getOperand(i)->canTrap()) |
| 84 | return true; |
| 85 | |
| 86 | // Otherwise, only specific operations can trap. |
| 87 | switch (CE->getOpcode()) { |
| 88 | default: |
| 89 | return false; |
Reid Spencer | 7e80b0b | 2006-10-26 06:15:43 +0000 | [diff] [blame] | 90 | case Instruction::UDiv: |
| 91 | case Instruction::SDiv: |
| 92 | case Instruction::FDiv: |
Reid Spencer | 7eb55b3 | 2006-11-02 01:53:59 +0000 | [diff] [blame] | 93 | case Instruction::URem: |
| 94 | case Instruction::SRem: |
| 95 | case Instruction::FRem: |
Chris Lattner | 23dd1f6 | 2006-10-20 00:27:06 +0000 | [diff] [blame] | 96 | // Div and rem can trap if the RHS is not known to be non-zero. |
| 97 | if (!isa<ConstantInt>(getOperand(1)) || getOperand(1)->isNullValue()) |
| 98 | return true; |
| 99 | return false; |
| 100 | } |
| 101 | } |
| 102 | |
Anton Korobeynikov | 7437b59 | 2009-03-29 17:13:18 +0000 | [diff] [blame] | 103 | /// ContainsRelocations - Return true if the constant value contains relocations |
| 104 | /// which cannot be resolved at compile time. Kind argument is used to filter |
| 105 | /// only 'interesting' sorts of relocations. |
| 106 | bool Constant::ContainsRelocations(unsigned Kind) const { |
| 107 | if (const GlobalValue* GV = dyn_cast<GlobalValue>(this)) { |
| 108 | bool isLocal = GV->hasLocalLinkage(); |
| 109 | if ((Kind & Reloc::Local) && isLocal) { |
| 110 | // Global has local linkage and 'local' kind of relocations are |
| 111 | // requested |
| 112 | return true; |
| 113 | } |
| 114 | |
| 115 | if ((Kind & Reloc::Global) && !isLocal) { |
| 116 | // Global has non-local linkage and 'global' kind of relocations are |
| 117 | // requested |
| 118 | return true; |
| 119 | } |
Anton Korobeynikov | 255a3cb | 2009-03-30 15:28:21 +0000 | [diff] [blame] | 120 | |
| 121 | return false; |
Anton Korobeynikov | 7437b59 | 2009-03-29 17:13:18 +0000 | [diff] [blame] | 122 | } |
| 123 | |
Evan Cheng | f9e003b | 2007-03-08 00:59:12 +0000 | [diff] [blame] | 124 | for (unsigned i = 0, e = getNumOperands(); i != e; ++i) |
Anton Korobeynikov | d5e8e93 | 2009-03-30 15:28:00 +0000 | [diff] [blame] | 125 | if (getOperand(i)->ContainsRelocations(Kind)) |
Evan Cheng | f9e003b | 2007-03-08 00:59:12 +0000 | [diff] [blame] | 126 | return true; |
Anton Korobeynikov | 7437b59 | 2009-03-29 17:13:18 +0000 | [diff] [blame] | 127 | |
Evan Cheng | f9e003b | 2007-03-08 00:59:12 +0000 | [diff] [blame] | 128 | return false; |
| 129 | } |
| 130 | |
Chris Lattner | 72e3958 | 2007-06-15 06:10:53 +0000 | [diff] [blame] | 131 | Constant *Constant::getAllOnesValue(const Type *Ty) { |
| 132 | if (const IntegerType* ITy = dyn_cast<IntegerType>(Ty)) |
| 133 | return ConstantInt::get(APInt::getAllOnesValue(ITy->getBitWidth())); |
| 134 | return ConstantVector::getAllOnesValue(cast<VectorType>(Ty)); |
| 135 | } |
Chris Lattner | b1585a9 | 2002-08-13 17:50:20 +0000 | [diff] [blame] | 136 | |
| 137 | // Static constructor to create an integral constant with all bits set |
Owen Anderson | 0d2de8c | 2009-06-20 00:24:58 +0000 | [diff] [blame] | 138 | ConstantInt *ConstantInt::getAllOnesValue(const Type *Ty) { |
Reid Spencer | 7a9c62b | 2007-01-12 07:05:14 +0000 | [diff] [blame] | 139 | if (const IntegerType* ITy = dyn_cast<IntegerType>(Ty)) |
Owen Anderson | 0d2de8c | 2009-06-20 00:24:58 +0000 | [diff] [blame] | 140 | return ConstantInt::get(APInt::getAllOnesValue(ITy->getBitWidth())); |
Reid Spencer | 7a9c62b | 2007-01-12 07:05:14 +0000 | [diff] [blame] | 141 | return 0; |
Chris Lattner | b1585a9 | 2002-08-13 17:50:20 +0000 | [diff] [blame] | 142 | } |
| 143 | |
Dan Gohman | 3097807 | 2007-05-24 14:36:04 +0000 | [diff] [blame] | 144 | /// @returns the value for a vector integer constant of the given type that |
Chris Lattner | ecab54c | 2007-01-04 01:49:26 +0000 | [diff] [blame] | 145 | /// has all its bits set to true. |
| 146 | /// @brief Get the all ones value |
Owen Anderson | 0d2de8c | 2009-06-20 00:24:58 +0000 | [diff] [blame] | 147 | ConstantVector *ConstantVector::getAllOnesValue(const VectorType *Ty) { |
Chris Lattner | ecab54c | 2007-01-04 01:49:26 +0000 | [diff] [blame] | 148 | std::vector<Constant*> Elts; |
| 149 | Elts.resize(Ty->getNumElements(), |
Owen Anderson | 0d2de8c | 2009-06-20 00:24:58 +0000 | [diff] [blame] | 150 | ConstantInt::getAllOnesValue(Ty->getElementType())); |
Dan Gohman | 3097807 | 2007-05-24 14:36:04 +0000 | [diff] [blame] | 151 | assert(Elts[0] && "Not a vector integer type!"); |
Owen Anderson | 0d2de8c | 2009-06-20 00:24:58 +0000 | [diff] [blame] | 152 | return cast<ConstantVector>(ConstantVector::get(Elts)); |
Chris Lattner | ecab54c | 2007-01-04 01:49:26 +0000 | [diff] [blame] | 153 | } |
| 154 | |
| 155 | |
Chris Lattner | 2105d66 | 2008-07-10 00:28:11 +0000 | [diff] [blame] | 156 | /// getVectorElements - This method, which is only valid on constant of vector |
| 157 | /// type, returns the elements of the vector in the specified smallvector. |
Chris Lattner | c5098a2 | 2008-07-14 05:10:41 +0000 | [diff] [blame] | 158 | /// This handles breaking down a vector undef into undef elements, etc. For |
| 159 | /// 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^] | 160 | void Constant::getVectorElements(LLVMContext &Context, |
| 161 | SmallVectorImpl<Constant*> &Elts) const { |
Chris Lattner | 2105d66 | 2008-07-10 00:28:11 +0000 | [diff] [blame] | 162 | assert(isa<VectorType>(getType()) && "Not a vector constant!"); |
| 163 | |
| 164 | if (const ConstantVector *CV = dyn_cast<ConstantVector>(this)) { |
| 165 | for (unsigned i = 0, e = CV->getNumOperands(); i != e; ++i) |
| 166 | Elts.push_back(CV->getOperand(i)); |
| 167 | return; |
| 168 | } |
| 169 | |
| 170 | const VectorType *VT = cast<VectorType>(getType()); |
| 171 | if (isa<ConstantAggregateZero>(this)) { |
| 172 | Elts.assign(VT->getNumElements(), |
Owen Anderson | 53a5221 | 2009-07-13 04:09:18 +0000 | [diff] [blame^] | 173 | Context.getNullValue(VT->getElementType())); |
Chris Lattner | 2105d66 | 2008-07-10 00:28:11 +0000 | [diff] [blame] | 174 | return; |
| 175 | } |
| 176 | |
Chris Lattner | c5098a2 | 2008-07-14 05:10:41 +0000 | [diff] [blame] | 177 | if (isa<UndefValue>(this)) { |
Owen Anderson | 53a5221 | 2009-07-13 04:09:18 +0000 | [diff] [blame^] | 178 | Elts.assign(VT->getNumElements(), Context.getUndef(VT->getElementType())); |
Chris Lattner | c5098a2 | 2008-07-14 05:10:41 +0000 | [diff] [blame] | 179 | return; |
| 180 | } |
| 181 | |
| 182 | // Unknown type, must be constant expr etc. |
Chris Lattner | 2105d66 | 2008-07-10 00:28:11 +0000 | [diff] [blame] | 183 | } |
| 184 | |
| 185 | |
| 186 | |
Chris Lattner | 2f7c963 | 2001-06-06 20:29:01 +0000 | [diff] [blame] | 187 | //===----------------------------------------------------------------------===// |
Chris Lattner | a80bf0b | 2007-02-20 06:39:57 +0000 | [diff] [blame] | 188 | // ConstantInt |
Chris Lattner | 2f7c963 | 2001-06-06 20:29:01 +0000 | [diff] [blame] | 189 | //===----------------------------------------------------------------------===// |
| 190 | |
Reid Spencer | b31bffe | 2007-02-26 23:54:03 +0000 | [diff] [blame] | 191 | ConstantInt::ConstantInt(const IntegerType *Ty, const APInt& V) |
Chris Lattner | 5db2f47 | 2007-02-20 05:55:46 +0000 | [diff] [blame] | 192 | : Constant(Ty, ConstantIntVal, 0, 0), Val(V) { |
Reid Spencer | b31bffe | 2007-02-26 23:54:03 +0000 | [diff] [blame] | 193 | assert(V.getBitWidth() == Ty->getBitWidth() && "Invalid constant for type"); |
Chris Lattner | 2f7c963 | 2001-06-06 20:29:01 +0000 | [diff] [blame] | 194 | } |
| 195 | |
Chris Lattner | a80bf0b | 2007-02-20 06:39:57 +0000 | [diff] [blame] | 196 | ConstantInt *ConstantInt::TheTrueVal = 0; |
| 197 | ConstantInt *ConstantInt::TheFalseVal = 0; |
| 198 | |
| 199 | namespace llvm { |
| 200 | void CleanupTrueFalse(void *) { |
| 201 | ConstantInt::ResetTrueFalse(); |
| 202 | } |
| 203 | } |
| 204 | |
| 205 | static ManagedCleanup<llvm::CleanupTrueFalse> TrueFalseCleanup; |
| 206 | |
| 207 | ConstantInt *ConstantInt::CreateTrueFalseVals(bool WhichOne) { |
| 208 | assert(TheTrueVal == 0 && TheFalseVal == 0); |
| 209 | TheTrueVal = get(Type::Int1Ty, 1); |
| 210 | TheFalseVal = get(Type::Int1Ty, 0); |
| 211 | |
| 212 | // Ensure that llvm_shutdown nulls out TheTrueVal/TheFalseVal. |
| 213 | TrueFalseCleanup.Register(); |
| 214 | |
| 215 | return WhichOne ? TheTrueVal : TheFalseVal; |
| 216 | } |
| 217 | |
| 218 | |
Chris Lattner | a80bf0b | 2007-02-20 06:39:57 +0000 | [diff] [blame] | 219 | namespace { |
Reid Spencer | b31bffe | 2007-02-26 23:54:03 +0000 | [diff] [blame] | 220 | struct DenseMapAPIntKeyInfo { |
| 221 | struct KeyTy { |
| 222 | APInt val; |
| 223 | const Type* type; |
| 224 | KeyTy(const APInt& V, const Type* Ty) : val(V), type(Ty) {} |
| 225 | KeyTy(const KeyTy& that) : val(that.val), type(that.type) {} |
| 226 | bool operator==(const KeyTy& that) const { |
| 227 | return type == that.type && this->val == that.val; |
| 228 | } |
| 229 | bool operator!=(const KeyTy& that) const { |
| 230 | return !this->operator==(that); |
| 231 | } |
| 232 | }; |
| 233 | static inline KeyTy getEmptyKey() { return KeyTy(APInt(1,0), 0); } |
| 234 | static inline KeyTy getTombstoneKey() { return KeyTy(APInt(1,1), 0); } |
Chris Lattner | a80bf0b | 2007-02-20 06:39:57 +0000 | [diff] [blame] | 235 | static unsigned getHashValue(const KeyTy &Key) { |
Chris Lattner | 0625bd6 | 2007-09-17 18:34:04 +0000 | [diff] [blame] | 236 | return DenseMapInfo<void*>::getHashValue(Key.type) ^ |
Reid Spencer | b31bffe | 2007-02-26 23:54:03 +0000 | [diff] [blame] | 237 | Key.val.getHashValue(); |
Chris Lattner | a80bf0b | 2007-02-20 06:39:57 +0000 | [diff] [blame] | 238 | } |
Chris Lattner | 0625bd6 | 2007-09-17 18:34:04 +0000 | [diff] [blame] | 239 | static bool isEqual(const KeyTy &LHS, const KeyTy &RHS) { |
| 240 | return LHS == RHS; |
| 241 | } |
Dale Johannesen | a719a60 | 2007-08-24 00:56:33 +0000 | [diff] [blame] | 242 | static bool isPod() { return false; } |
Chris Lattner | a80bf0b | 2007-02-20 06:39:57 +0000 | [diff] [blame] | 243 | }; |
| 244 | } |
| 245 | |
| 246 | |
Reid Spencer | b31bffe | 2007-02-26 23:54:03 +0000 | [diff] [blame] | 247 | typedef DenseMap<DenseMapAPIntKeyInfo::KeyTy, ConstantInt*, |
| 248 | DenseMapAPIntKeyInfo> IntMapTy; |
Chris Lattner | a80bf0b | 2007-02-20 06:39:57 +0000 | [diff] [blame] | 249 | static ManagedStatic<IntMapTy> IntConstants; |
| 250 | |
Dan Gohman | 7ccc52f | 2009-06-15 22:12:54 +0000 | [diff] [blame] | 251 | ConstantInt *ConstantInt::get(const IntegerType *Ty, |
Owen Anderson | 0d2de8c | 2009-06-20 00:24:58 +0000 | [diff] [blame] | 252 | uint64_t V, bool isSigned) { |
| 253 | return get(APInt(Ty->getBitWidth(), V, isSigned)); |
Dan Gohman | 7ccc52f | 2009-06-15 22:12:54 +0000 | [diff] [blame] | 254 | } |
| 255 | |
Owen Anderson | 0d2de8c | 2009-06-20 00:24:58 +0000 | [diff] [blame] | 256 | Constant *ConstantInt::get(const Type *Ty, uint64_t V, bool isSigned) { |
Dan Gohman | 7ccc52f | 2009-06-15 22:12:54 +0000 | [diff] [blame] | 257 | Constant *C = get(cast<IntegerType>(Ty->getScalarType()), V, isSigned); |
| 258 | |
| 259 | // For vectors, broadcast the value. |
| 260 | if (const VectorType *VTy = dyn_cast<VectorType>(Ty)) |
| 261 | return |
Owen Anderson | 0d2de8c | 2009-06-20 00:24:58 +0000 | [diff] [blame] | 262 | ConstantVector::get(std::vector<Constant *>(VTy->getNumElements(), C)); |
Dan Gohman | 7ccc52f | 2009-06-15 22:12:54 +0000 | [diff] [blame] | 263 | |
| 264 | return C; |
Reid Spencer | b31bffe | 2007-02-26 23:54:03 +0000 | [diff] [blame] | 265 | } |
| 266 | |
Reid Spencer | d1bbfa5 | 2007-03-01 19:30:34 +0000 | [diff] [blame] | 267 | // Get a ConstantInt from an APInt. Note that the value stored in the DenseMap |
Dan Gohman | b3efe03 | 2008-02-07 02:30:40 +0000 | [diff] [blame] | 268 | // as the key, is a DenseMapAPIntKeyInfo::KeyTy which has provided the |
Reid Spencer | b31bffe | 2007-02-26 23:54:03 +0000 | [diff] [blame] | 269 | // operator== and operator!= to ensure that the DenseMap doesn't attempt to |
| 270 | // compare APInt's of different widths, which would violate an APInt class |
| 271 | // invariant which generates an assertion. |
Owen Anderson | 0d2de8c | 2009-06-20 00:24:58 +0000 | [diff] [blame] | 272 | ConstantInt *ConstantInt::get(const APInt& V) { |
Reid Spencer | d1bbfa5 | 2007-03-01 19:30:34 +0000 | [diff] [blame] | 273 | // Get the corresponding integer type for the bit width of the value. |
| 274 | const IntegerType *ITy = IntegerType::get(V.getBitWidth()); |
Reid Spencer | b31bffe | 2007-02-26 23:54:03 +0000 | [diff] [blame] | 275 | // get an existing value or the insertion position |
Reid Spencer | d1bbfa5 | 2007-03-01 19:30:34 +0000 | [diff] [blame] | 276 | DenseMapAPIntKeyInfo::KeyTy Key(V, ITy); |
Owen Anderson | 2d7231d | 2009-06-17 18:40:29 +0000 | [diff] [blame] | 277 | |
Owen Anderson | 0d2de8c | 2009-06-20 00:24:58 +0000 | [diff] [blame] | 278 | ConstantsLock->reader_acquire(); |
Owen Anderson | d830eb8 | 2009-06-18 19:10:19 +0000 | [diff] [blame] | 279 | ConstantInt *&Slot = (*IntConstants)[Key]; |
Owen Anderson | 0d2de8c | 2009-06-20 00:24:58 +0000 | [diff] [blame] | 280 | ConstantsLock->reader_release(); |
Owen Anderson | 2d7231d | 2009-06-17 18:40:29 +0000 | [diff] [blame] | 281 | |
Owen Anderson | d830eb8 | 2009-06-18 19:10:19 +0000 | [diff] [blame] | 282 | if (!Slot) { |
Owen Anderson | 5c96ef7 | 2009-07-07 18:33:04 +0000 | [diff] [blame] | 283 | sys::SmartScopedWriter<true> Writer(*ConstantsLock); |
Owen Anderson | 0d2de8c | 2009-06-20 00:24:58 +0000 | [diff] [blame] | 284 | ConstantInt *&NewSlot = (*IntConstants)[Key]; |
| 285 | if (!Slot) { |
| 286 | NewSlot = new ConstantInt(ITy, V); |
Owen Anderson | 2d7231d | 2009-06-17 18:40:29 +0000 | [diff] [blame] | 287 | } |
Owen Anderson | 0d2de8c | 2009-06-20 00:24:58 +0000 | [diff] [blame] | 288 | |
| 289 | return NewSlot; |
| 290 | } else { |
| 291 | return Slot; |
Owen Anderson | 2d7231d | 2009-06-17 18:40:29 +0000 | [diff] [blame] | 292 | } |
Chris Lattner | a80bf0b | 2007-02-20 06:39:57 +0000 | [diff] [blame] | 293 | } |
| 294 | |
Owen Anderson | 0d2de8c | 2009-06-20 00:24:58 +0000 | [diff] [blame] | 295 | Constant *ConstantInt::get(const Type *Ty, const APInt &V) { |
| 296 | ConstantInt *C = ConstantInt::get(V); |
Dan Gohman | 7ccc52f | 2009-06-15 22:12:54 +0000 | [diff] [blame] | 297 | assert(C->getType() == Ty->getScalarType() && |
| 298 | "ConstantInt type doesn't match the type implied by its value!"); |
| 299 | |
| 300 | // For vectors, broadcast the value. |
| 301 | if (const VectorType *VTy = dyn_cast<VectorType>(Ty)) |
| 302 | return |
Owen Anderson | 0d2de8c | 2009-06-20 00:24:58 +0000 | [diff] [blame] | 303 | ConstantVector::get(std::vector<Constant *>(VTy->getNumElements(), C)); |
Dan Gohman | 7ccc52f | 2009-06-15 22:12:54 +0000 | [diff] [blame] | 304 | |
| 305 | return C; |
| 306 | } |
| 307 | |
Chris Lattner | a80bf0b | 2007-02-20 06:39:57 +0000 | [diff] [blame] | 308 | //===----------------------------------------------------------------------===// |
Chris Lattner | c6ee77d | 2007-02-20 07:17:17 +0000 | [diff] [blame] | 309 | // ConstantFP |
Chris Lattner | a80bf0b | 2007-02-20 06:39:57 +0000 | [diff] [blame] | 310 | //===----------------------------------------------------------------------===// |
| 311 | |
Chris Lattner | 98bd939 | 2008-04-09 06:38:30 +0000 | [diff] [blame] | 312 | static const fltSemantics *TypeToFloatSemantics(const Type *Ty) { |
| 313 | if (Ty == Type::FloatTy) |
| 314 | return &APFloat::IEEEsingle; |
| 315 | if (Ty == Type::DoubleTy) |
| 316 | return &APFloat::IEEEdouble; |
| 317 | if (Ty == Type::X86_FP80Ty) |
| 318 | return &APFloat::x87DoubleExtended; |
| 319 | else if (Ty == Type::FP128Ty) |
| 320 | return &APFloat::IEEEquad; |
| 321 | |
| 322 | assert(Ty == Type::PPC_FP128Ty && "Unknown FP format"); |
| 323 | return &APFloat::PPCDoubleDouble; |
| 324 | } |
| 325 | |
Dale Johannesen | d246b2c | 2007-08-30 00:23:21 +0000 | [diff] [blame] | 326 | ConstantFP::ConstantFP(const Type *Ty, const APFloat& V) |
| 327 | : Constant(Ty, ConstantFPVal, 0, 0), Val(V) { |
Chris Lattner | 98bd939 | 2008-04-09 06:38:30 +0000 | [diff] [blame] | 328 | assert(&V.getSemantics() == TypeToFloatSemantics(Ty) && |
| 329 | "FP type Mismatch"); |
Chris Lattner | 2f7c963 | 2001-06-06 20:29:01 +0000 | [diff] [blame] | 330 | } |
| 331 | |
Chris Lattner | c6ee77d | 2007-02-20 07:17:17 +0000 | [diff] [blame] | 332 | bool ConstantFP::isNullValue() const { |
Dale Johannesen | a719a60 | 2007-08-24 00:56:33 +0000 | [diff] [blame] | 333 | return Val.isZero() && !Val.isNegative(); |
Chris Lattner | c6ee77d | 2007-02-20 07:17:17 +0000 | [diff] [blame] | 334 | } |
| 335 | |
Dale Johannesen | d246b2c | 2007-08-30 00:23:21 +0000 | [diff] [blame] | 336 | bool ConstantFP::isExactlyValue(const APFloat& V) const { |
| 337 | return Val.bitwiseIsEqual(V); |
Chris Lattner | c6ee77d | 2007-02-20 07:17:17 +0000 | [diff] [blame] | 338 | } |
| 339 | |
Chris Lattner | c6ee77d | 2007-02-20 07:17:17 +0000 | [diff] [blame] | 340 | namespace { |
Dale Johannesen | a719a60 | 2007-08-24 00:56:33 +0000 | [diff] [blame] | 341 | struct DenseMapAPFloatKeyInfo { |
Dale Johannesen | bdea32d | 2007-08-24 22:09:56 +0000 | [diff] [blame] | 342 | struct KeyTy { |
| 343 | APFloat val; |
| 344 | KeyTy(const APFloat& V) : val(V){} |
| 345 | KeyTy(const KeyTy& that) : val(that.val) {} |
| 346 | bool operator==(const KeyTy& that) const { |
| 347 | return this->val.bitwiseIsEqual(that.val); |
| 348 | } |
| 349 | bool operator!=(const KeyTy& that) const { |
| 350 | return !this->operator==(that); |
| 351 | } |
| 352 | }; |
| 353 | static inline KeyTy getEmptyKey() { |
| 354 | return KeyTy(APFloat(APFloat::Bogus,1)); |
Reid Spencer | b31bffe | 2007-02-26 23:54:03 +0000 | [diff] [blame] | 355 | } |
Dale Johannesen | bdea32d | 2007-08-24 22:09:56 +0000 | [diff] [blame] | 356 | static inline KeyTy getTombstoneKey() { |
| 357 | return KeyTy(APFloat(APFloat::Bogus,2)); |
Chris Lattner | c6ee77d | 2007-02-20 07:17:17 +0000 | [diff] [blame] | 358 | } |
Dale Johannesen | bdea32d | 2007-08-24 22:09:56 +0000 | [diff] [blame] | 359 | static unsigned getHashValue(const KeyTy &Key) { |
| 360 | return Key.val.getHashValue(); |
Dale Johannesen | a719a60 | 2007-08-24 00:56:33 +0000 | [diff] [blame] | 361 | } |
Chris Lattner | 0625bd6 | 2007-09-17 18:34:04 +0000 | [diff] [blame] | 362 | static bool isEqual(const KeyTy &LHS, const KeyTy &RHS) { |
| 363 | return LHS == RHS; |
| 364 | } |
Dale Johannesen | a719a60 | 2007-08-24 00:56:33 +0000 | [diff] [blame] | 365 | static bool isPod() { return false; } |
Chris Lattner | c6ee77d | 2007-02-20 07:17:17 +0000 | [diff] [blame] | 366 | }; |
| 367 | } |
| 368 | |
| 369 | //---- ConstantFP::get() implementation... |
| 370 | // |
Dale Johannesen | bdea32d | 2007-08-24 22:09:56 +0000 | [diff] [blame] | 371 | typedef DenseMap<DenseMapAPFloatKeyInfo::KeyTy, ConstantFP*, |
Dale Johannesen | a719a60 | 2007-08-24 00:56:33 +0000 | [diff] [blame] | 372 | DenseMapAPFloatKeyInfo> FPMapTy; |
Chris Lattner | c6ee77d | 2007-02-20 07:17:17 +0000 | [diff] [blame] | 373 | |
Dale Johannesen | a719a60 | 2007-08-24 00:56:33 +0000 | [diff] [blame] | 374 | static ManagedStatic<FPMapTy> FPConstants; |
Chris Lattner | c6ee77d | 2007-02-20 07:17:17 +0000 | [diff] [blame] | 375 | |
Owen Anderson | 0d2de8c | 2009-06-20 00:24:58 +0000 | [diff] [blame] | 376 | ConstantFP *ConstantFP::get(const APFloat &V) { |
Dale Johannesen | d246b2c | 2007-08-30 00:23:21 +0000 | [diff] [blame] | 377 | DenseMapAPFloatKeyInfo::KeyTy Key(V); |
Chris Lattner | b5b3e31 | 2008-04-09 00:45:01 +0000 | [diff] [blame] | 378 | |
Owen Anderson | 0d2de8c | 2009-06-20 00:24:58 +0000 | [diff] [blame] | 379 | ConstantsLock->reader_acquire(); |
Owen Anderson | d830eb8 | 2009-06-18 19:10:19 +0000 | [diff] [blame] | 380 | ConstantFP *&Slot = (*FPConstants)[Key]; |
Owen Anderson | 0d2de8c | 2009-06-20 00:24:58 +0000 | [diff] [blame] | 381 | ConstantsLock->reader_release(); |
Owen Anderson | 2d7231d | 2009-06-17 18:40:29 +0000 | [diff] [blame] | 382 | |
Owen Anderson | d830eb8 | 2009-06-18 19:10:19 +0000 | [diff] [blame] | 383 | if (!Slot) { |
Owen Anderson | 5c96ef7 | 2009-07-07 18:33:04 +0000 | [diff] [blame] | 384 | sys::SmartScopedWriter<true> Writer(*ConstantsLock); |
Owen Anderson | 0d2de8c | 2009-06-20 00:24:58 +0000 | [diff] [blame] | 385 | ConstantFP *&NewSlot = (*FPConstants)[Key]; |
| 386 | if (!NewSlot) { |
Owen Anderson | d830eb8 | 2009-06-18 19:10:19 +0000 | [diff] [blame] | 387 | const Type *Ty; |
| 388 | if (&V.getSemantics() == &APFloat::IEEEsingle) |
| 389 | Ty = Type::FloatTy; |
| 390 | else if (&V.getSemantics() == &APFloat::IEEEdouble) |
| 391 | Ty = Type::DoubleTy; |
| 392 | else if (&V.getSemantics() == &APFloat::x87DoubleExtended) |
| 393 | Ty = Type::X86_FP80Ty; |
| 394 | else if (&V.getSemantics() == &APFloat::IEEEquad) |
| 395 | Ty = Type::FP128Ty; |
| 396 | else { |
| 397 | assert(&V.getSemantics() == &APFloat::PPCDoubleDouble && |
| 398 | "Unknown FP format"); |
| 399 | Ty = Type::PPC_FP128Ty; |
Owen Anderson | 2d7231d | 2009-06-17 18:40:29 +0000 | [diff] [blame] | 400 | } |
Owen Anderson | 0d2de8c | 2009-06-20 00:24:58 +0000 | [diff] [blame] | 401 | NewSlot = new ConstantFP(Ty, V); |
Owen Anderson | 2d7231d | 2009-06-17 18:40:29 +0000 | [diff] [blame] | 402 | } |
Owen Anderson | 0d2de8c | 2009-06-20 00:24:58 +0000 | [diff] [blame] | 403 | |
| 404 | return NewSlot; |
Chris Lattner | b5b3e31 | 2008-04-09 00:45:01 +0000 | [diff] [blame] | 405 | } |
Owen Anderson | d830eb8 | 2009-06-18 19:10:19 +0000 | [diff] [blame] | 406 | |
| 407 | return Slot; |
Dale Johannesen | d246b2c | 2007-08-30 00:23:21 +0000 | [diff] [blame] | 408 | } |
Chris Lattner | c6ee77d | 2007-02-20 07:17:17 +0000 | [diff] [blame] | 409 | |
Chris Lattner | 98bd939 | 2008-04-09 06:38:30 +0000 | [diff] [blame] | 410 | /// get() - This returns a constant fp for the specified value in the |
| 411 | /// specified type. This should only be used for simple constant values like |
| 412 | /// 2.0/1.0 etc, that are known-valid both as double and as the target format. |
Owen Anderson | 0d2de8c | 2009-06-20 00:24:58 +0000 | [diff] [blame] | 413 | Constant *ConstantFP::get(const Type *Ty, double V) { |
Chris Lattner | 98bd939 | 2008-04-09 06:38:30 +0000 | [diff] [blame] | 414 | APFloat FV(V); |
Dale Johannesen | 4f0bd68 | 2008-10-09 23:00:39 +0000 | [diff] [blame] | 415 | bool ignored; |
Dan Gohman | 7ccc52f | 2009-06-15 22:12:54 +0000 | [diff] [blame] | 416 | FV.convert(*TypeToFloatSemantics(Ty->getScalarType()), |
| 417 | APFloat::rmNearestTiesToEven, &ignored); |
Owen Anderson | 0d2de8c | 2009-06-20 00:24:58 +0000 | [diff] [blame] | 418 | Constant *C = get(FV); |
Dan Gohman | 7ccc52f | 2009-06-15 22:12:54 +0000 | [diff] [blame] | 419 | |
| 420 | // For vectors, broadcast the value. |
| 421 | if (const VectorType *VTy = dyn_cast<VectorType>(Ty)) |
| 422 | return |
Owen Anderson | 0d2de8c | 2009-06-20 00:24:58 +0000 | [diff] [blame] | 423 | ConstantVector::get(std::vector<Constant *>(VTy->getNumElements(), C)); |
Dan Gohman | 7ccc52f | 2009-06-15 22:12:54 +0000 | [diff] [blame] | 424 | |
| 425 | return C; |
Chris Lattner | 98bd939 | 2008-04-09 06:38:30 +0000 | [diff] [blame] | 426 | } |
| 427 | |
Chris Lattner | c6ee77d | 2007-02-20 07:17:17 +0000 | [diff] [blame] | 428 | //===----------------------------------------------------------------------===// |
| 429 | // ConstantXXX Classes |
| 430 | //===----------------------------------------------------------------------===// |
| 431 | |
| 432 | |
Chris Lattner | 3462ae3 | 2001-12-03 22:26:30 +0000 | [diff] [blame] | 433 | ConstantArray::ConstantArray(const ArrayType *T, |
Chris Lattner | d0df99c | 2005-01-29 00:34:39 +0000 | [diff] [blame] | 434 | const std::vector<Constant*> &V) |
Gabor Greif | f6caff66 | 2008-05-10 08:32:32 +0000 | [diff] [blame] | 435 | : Constant(T, ConstantArrayVal, |
| 436 | OperandTraits<ConstantArray>::op_end(this) - V.size(), |
| 437 | V.size()) { |
Alkis Evlogimenos | 0507ffe | 2004-09-15 02:32:15 +0000 | [diff] [blame] | 438 | assert(V.size() == T->getNumElements() && |
| 439 | "Invalid initializer vector for constant array"); |
Chris Lattner | d0df99c | 2005-01-29 00:34:39 +0000 | [diff] [blame] | 440 | Use *OL = OperandList; |
Chris Lattner | 0144fad | 2005-10-03 21:56:24 +0000 | [diff] [blame] | 441 | for (std::vector<Constant*>::const_iterator I = V.begin(), E = V.end(); |
| 442 | I != E; ++I, ++OL) { |
Chris Lattner | 20a2445 | 2005-10-07 05:23:36 +0000 | [diff] [blame] | 443 | Constant *C = *I; |
| 444 | assert((C->getType() == T->getElementType() || |
Alkis Evlogimenos | cb031d9 | 2004-09-10 04:16:59 +0000 | [diff] [blame] | 445 | (T->isAbstract() && |
Chris Lattner | 20a2445 | 2005-10-07 05:23:36 +0000 | [diff] [blame] | 446 | C->getType()->getTypeID() == T->getElementType()->getTypeID())) && |
Alkis Evlogimenos | cb031d9 | 2004-09-10 04:16:59 +0000 | [diff] [blame] | 447 | "Initializer for array element doesn't match array element type!"); |
Gabor Greif | 2d3024d | 2008-05-26 21:33:52 +0000 | [diff] [blame] | 448 | *OL = C; |
Chris Lattner | 2f7c963 | 2001-06-06 20:29:01 +0000 | [diff] [blame] | 449 | } |
| 450 | } |
| 451 | |
Chris Lattner | d0df99c | 2005-01-29 00:34:39 +0000 | [diff] [blame] | 452 | |
Chris Lattner | 3462ae3 | 2001-12-03 22:26:30 +0000 | [diff] [blame] | 453 | ConstantStruct::ConstantStruct(const StructType *T, |
Chris Lattner | d0df99c | 2005-01-29 00:34:39 +0000 | [diff] [blame] | 454 | const std::vector<Constant*> &V) |
Gabor Greif | f6caff66 | 2008-05-10 08:32:32 +0000 | [diff] [blame] | 455 | : Constant(T, ConstantStructVal, |
| 456 | OperandTraits<ConstantStruct>::op_end(this) - V.size(), |
| 457 | V.size()) { |
Chris Lattner | ac6db75 | 2004-02-09 04:37:31 +0000 | [diff] [blame] | 458 | assert(V.size() == T->getNumElements() && |
Vikram S. Adve | 4e537b2 | 2002-07-14 23:13:17 +0000 | [diff] [blame] | 459 | "Invalid initializer vector for constant structure"); |
Chris Lattner | d0df99c | 2005-01-29 00:34:39 +0000 | [diff] [blame] | 460 | Use *OL = OperandList; |
Chris Lattner | 0144fad | 2005-10-03 21:56:24 +0000 | [diff] [blame] | 461 | for (std::vector<Constant*>::const_iterator I = V.begin(), E = V.end(); |
| 462 | I != E; ++I, ++OL) { |
Chris Lattner | 20a2445 | 2005-10-07 05:23:36 +0000 | [diff] [blame] | 463 | Constant *C = *I; |
| 464 | assert((C->getType() == T->getElementType(I-V.begin()) || |
Chris Lattner | 0144fad | 2005-10-03 21:56:24 +0000 | [diff] [blame] | 465 | ((T->getElementType(I-V.begin())->isAbstract() || |
Chris Lattner | 20a2445 | 2005-10-07 05:23:36 +0000 | [diff] [blame] | 466 | C->getType()->isAbstract()) && |
Chris Lattner | 0144fad | 2005-10-03 21:56:24 +0000 | [diff] [blame] | 467 | T->getElementType(I-V.begin())->getTypeID() == |
Chris Lattner | 20a2445 | 2005-10-07 05:23:36 +0000 | [diff] [blame] | 468 | C->getType()->getTypeID())) && |
Chris Lattner | 93c8f14 | 2003-06-02 17:42:47 +0000 | [diff] [blame] | 469 | "Initializer for struct element doesn't match struct 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 | |
Chris Lattner | d0df99c | 2005-01-29 00:34:39 +0000 | [diff] [blame] | 474 | |
Reid Spencer | d84d35b | 2007-02-15 02:26:10 +0000 | [diff] [blame] | 475 | ConstantVector::ConstantVector(const VectorType *T, |
Chris Lattner | d0df99c | 2005-01-29 00:34:39 +0000 | [diff] [blame] | 476 | const std::vector<Constant*> &V) |
Gabor Greif | f6caff66 | 2008-05-10 08:32:32 +0000 | [diff] [blame] | 477 | : Constant(T, ConstantVectorVal, |
| 478 | OperandTraits<ConstantVector>::op_end(this) - V.size(), |
| 479 | V.size()) { |
Chris Lattner | d0df99c | 2005-01-29 00:34:39 +0000 | [diff] [blame] | 480 | Use *OL = OperandList; |
Chris Lattner | 0144fad | 2005-10-03 21:56:24 +0000 | [diff] [blame] | 481 | for (std::vector<Constant*>::const_iterator I = V.begin(), E = V.end(); |
| 482 | I != E; ++I, ++OL) { |
Chris Lattner | 20a2445 | 2005-10-07 05:23:36 +0000 | [diff] [blame] | 483 | Constant *C = *I; |
| 484 | assert((C->getType() == T->getElementType() || |
Alkis Evlogimenos | cb031d9 | 2004-09-10 04:16:59 +0000 | [diff] [blame] | 485 | (T->isAbstract() && |
Chris Lattner | 20a2445 | 2005-10-07 05:23:36 +0000 | [diff] [blame] | 486 | C->getType()->getTypeID() == T->getElementType()->getTypeID())) && |
Dan Gohman | 3097807 | 2007-05-24 14:36:04 +0000 | [diff] [blame] | 487 | "Initializer for vector element doesn't match vector element type!"); |
Gabor Greif | 2d3024d | 2008-05-26 21:33:52 +0000 | [diff] [blame] | 488 | *OL = C; |
Brian Gaeke | 0220904 | 2004-08-20 06:00:58 +0000 | [diff] [blame] | 489 | } |
| 490 | } |
| 491 | |
Vikram S. Adve | 4e537b2 | 2002-07-14 23:13:17 +0000 | [diff] [blame] | 492 | |
Gabor Greif | f6caff66 | 2008-05-10 08:32:32 +0000 | [diff] [blame] | 493 | namespace llvm { |
Gordon Henriksen | 14a5569 | 2007-12-10 02:14:30 +0000 | [diff] [blame] | 494 | // We declare several classes private to this file, so use an anonymous |
| 495 | // namespace |
| 496 | namespace { |
Reid Spencer | 10fbf0e | 2006-12-03 05:48:19 +0000 | [diff] [blame] | 497 | |
Gordon Henriksen | 14a5569 | 2007-12-10 02:14:30 +0000 | [diff] [blame] | 498 | /// UnaryConstantExpr - This class is private to Constants.cpp, and is used |
| 499 | /// behind the scenes to implement unary constant exprs. |
| 500 | class VISIBILITY_HIDDEN UnaryConstantExpr : public ConstantExpr { |
Gabor Greif | e9ecc68 | 2008-04-06 20:25:17 +0000 | [diff] [blame] | 501 | void *operator new(size_t, unsigned); // DO NOT IMPLEMENT |
Gordon Henriksen | 14a5569 | 2007-12-10 02:14:30 +0000 | [diff] [blame] | 502 | public: |
Gabor Greif | e9ecc68 | 2008-04-06 20:25:17 +0000 | [diff] [blame] | 503 | // allocate space for exactly one operand |
| 504 | void *operator new(size_t s) { |
| 505 | return User::operator new(s, 1); |
| 506 | } |
Gordon Henriksen | 14a5569 | 2007-12-10 02:14:30 +0000 | [diff] [blame] | 507 | UnaryConstantExpr(unsigned Opcode, Constant *C, const Type *Ty) |
Gabor Greif | f6caff66 | 2008-05-10 08:32:32 +0000 | [diff] [blame] | 508 | : ConstantExpr(Ty, Opcode, &Op<0>(), 1) { |
| 509 | Op<0>() = C; |
| 510 | } |
| 511 | /// Transparently provide more efficient getOperand methods. |
| 512 | DECLARE_TRANSPARENT_OPERAND_ACCESSORS(Value); |
Gordon Henriksen | 14a5569 | 2007-12-10 02:14:30 +0000 | [diff] [blame] | 513 | }; |
Reid Spencer | 10fbf0e | 2006-12-03 05:48:19 +0000 | [diff] [blame] | 514 | |
Gordon Henriksen | 14a5569 | 2007-12-10 02:14:30 +0000 | [diff] [blame] | 515 | /// BinaryConstantExpr - This class is private to Constants.cpp, and is used |
| 516 | /// behind the scenes to implement binary constant exprs. |
| 517 | class VISIBILITY_HIDDEN BinaryConstantExpr : public ConstantExpr { |
Gabor Greif | e9ecc68 | 2008-04-06 20:25:17 +0000 | [diff] [blame] | 518 | void *operator new(size_t, unsigned); // DO NOT IMPLEMENT |
Gordon Henriksen | 14a5569 | 2007-12-10 02:14:30 +0000 | [diff] [blame] | 519 | public: |
Gabor Greif | e9ecc68 | 2008-04-06 20:25:17 +0000 | [diff] [blame] | 520 | // allocate space for exactly two operands |
| 521 | void *operator new(size_t s) { |
| 522 | return User::operator new(s, 2); |
| 523 | } |
Gordon Henriksen | 14a5569 | 2007-12-10 02:14:30 +0000 | [diff] [blame] | 524 | BinaryConstantExpr(unsigned Opcode, Constant *C1, Constant *C2) |
Gabor Greif | f6caff66 | 2008-05-10 08:32:32 +0000 | [diff] [blame] | 525 | : ConstantExpr(C1->getType(), Opcode, &Op<0>(), 2) { |
Gabor Greif | 2d3024d | 2008-05-26 21:33:52 +0000 | [diff] [blame] | 526 | Op<0>() = C1; |
| 527 | Op<1>() = C2; |
Gordon Henriksen | 14a5569 | 2007-12-10 02:14:30 +0000 | [diff] [blame] | 528 | } |
Gabor Greif | f6caff66 | 2008-05-10 08:32:32 +0000 | [diff] [blame] | 529 | /// Transparently provide more efficient getOperand methods. |
| 530 | DECLARE_TRANSPARENT_OPERAND_ACCESSORS(Value); |
Gordon Henriksen | 14a5569 | 2007-12-10 02:14:30 +0000 | [diff] [blame] | 531 | }; |
Vikram S. Adve | 4e537b2 | 2002-07-14 23:13:17 +0000 | [diff] [blame] | 532 | |
Gordon Henriksen | 14a5569 | 2007-12-10 02:14:30 +0000 | [diff] [blame] | 533 | /// SelectConstantExpr - This class is private to Constants.cpp, and is used |
| 534 | /// behind the scenes to implement select constant exprs. |
| 535 | class VISIBILITY_HIDDEN SelectConstantExpr : public ConstantExpr { |
Gabor Greif | e9ecc68 | 2008-04-06 20:25:17 +0000 | [diff] [blame] | 536 | void *operator new(size_t, unsigned); // DO NOT IMPLEMENT |
Gordon Henriksen | 14a5569 | 2007-12-10 02:14:30 +0000 | [diff] [blame] | 537 | public: |
Gabor Greif | e9ecc68 | 2008-04-06 20:25:17 +0000 | [diff] [blame] | 538 | // allocate space for exactly three operands |
| 539 | void *operator new(size_t s) { |
| 540 | return User::operator new(s, 3); |
| 541 | } |
Gordon Henriksen | 14a5569 | 2007-12-10 02:14:30 +0000 | [diff] [blame] | 542 | SelectConstantExpr(Constant *C1, Constant *C2, Constant *C3) |
Gabor Greif | f6caff66 | 2008-05-10 08:32:32 +0000 | [diff] [blame] | 543 | : ConstantExpr(C2->getType(), Instruction::Select, &Op<0>(), 3) { |
Gabor Greif | 2d3024d | 2008-05-26 21:33:52 +0000 | [diff] [blame] | 544 | Op<0>() = C1; |
| 545 | Op<1>() = C2; |
| 546 | Op<2>() = C3; |
Gordon Henriksen | 14a5569 | 2007-12-10 02:14:30 +0000 | [diff] [blame] | 547 | } |
Gabor Greif | f6caff66 | 2008-05-10 08:32:32 +0000 | [diff] [blame] | 548 | /// Transparently provide more efficient getOperand methods. |
| 549 | DECLARE_TRANSPARENT_OPERAND_ACCESSORS(Value); |
Gordon Henriksen | 14a5569 | 2007-12-10 02:14:30 +0000 | [diff] [blame] | 550 | }; |
Chris Lattner | d0df99c | 2005-01-29 00:34:39 +0000 | [diff] [blame] | 551 | |
Gordon Henriksen | 14a5569 | 2007-12-10 02:14:30 +0000 | [diff] [blame] | 552 | /// ExtractElementConstantExpr - This class is private to |
| 553 | /// Constants.cpp, and is used behind the scenes to implement |
| 554 | /// extractelement constant exprs. |
| 555 | class VISIBILITY_HIDDEN ExtractElementConstantExpr : public ConstantExpr { |
Gabor Greif | e9ecc68 | 2008-04-06 20:25:17 +0000 | [diff] [blame] | 556 | void *operator new(size_t, unsigned); // DO NOT IMPLEMENT |
Gordon Henriksen | 14a5569 | 2007-12-10 02:14:30 +0000 | [diff] [blame] | 557 | public: |
Gabor Greif | e9ecc68 | 2008-04-06 20:25:17 +0000 | [diff] [blame] | 558 | // allocate space for exactly two operands |
| 559 | void *operator new(size_t s) { |
| 560 | return User::operator new(s, 2); |
| 561 | } |
Gordon Henriksen | 14a5569 | 2007-12-10 02:14:30 +0000 | [diff] [blame] | 562 | ExtractElementConstantExpr(Constant *C1, Constant *C2) |
| 563 | : ConstantExpr(cast<VectorType>(C1->getType())->getElementType(), |
Gabor Greif | f6caff66 | 2008-05-10 08:32:32 +0000 | [diff] [blame] | 564 | Instruction::ExtractElement, &Op<0>(), 2) { |
Gabor Greif | 2d3024d | 2008-05-26 21:33:52 +0000 | [diff] [blame] | 565 | Op<0>() = C1; |
| 566 | Op<1>() = C2; |
Gordon Henriksen | 14a5569 | 2007-12-10 02:14:30 +0000 | [diff] [blame] | 567 | } |
Gabor Greif | f6caff66 | 2008-05-10 08:32:32 +0000 | [diff] [blame] | 568 | /// Transparently provide more efficient getOperand methods. |
| 569 | DECLARE_TRANSPARENT_OPERAND_ACCESSORS(Value); |
Gordon Henriksen | 14a5569 | 2007-12-10 02:14:30 +0000 | [diff] [blame] | 570 | }; |
Robert Bocchino | 2300448 | 2006-01-10 19:05:34 +0000 | [diff] [blame] | 571 | |
Gordon Henriksen | 14a5569 | 2007-12-10 02:14:30 +0000 | [diff] [blame] | 572 | /// InsertElementConstantExpr - This class is private to |
| 573 | /// Constants.cpp, and is used behind the scenes to implement |
| 574 | /// insertelement constant exprs. |
| 575 | class VISIBILITY_HIDDEN InsertElementConstantExpr : public ConstantExpr { |
Gabor Greif | e9ecc68 | 2008-04-06 20:25:17 +0000 | [diff] [blame] | 576 | void *operator new(size_t, unsigned); // DO NOT IMPLEMENT |
Gordon Henriksen | 14a5569 | 2007-12-10 02:14:30 +0000 | [diff] [blame] | 577 | public: |
Gabor Greif | e9ecc68 | 2008-04-06 20:25:17 +0000 | [diff] [blame] | 578 | // allocate space for exactly three operands |
| 579 | void *operator new(size_t s) { |
| 580 | return User::operator new(s, 3); |
| 581 | } |
Gordon Henriksen | 14a5569 | 2007-12-10 02:14:30 +0000 | [diff] [blame] | 582 | InsertElementConstantExpr(Constant *C1, Constant *C2, Constant *C3) |
| 583 | : ConstantExpr(C1->getType(), Instruction::InsertElement, |
Gabor Greif | f6caff66 | 2008-05-10 08:32:32 +0000 | [diff] [blame] | 584 | &Op<0>(), 3) { |
Gabor Greif | 2d3024d | 2008-05-26 21:33:52 +0000 | [diff] [blame] | 585 | Op<0>() = C1; |
| 586 | Op<1>() = C2; |
| 587 | Op<2>() = C3; |
Gordon Henriksen | 14a5569 | 2007-12-10 02:14:30 +0000 | [diff] [blame] | 588 | } |
Gabor Greif | f6caff66 | 2008-05-10 08:32:32 +0000 | [diff] [blame] | 589 | /// Transparently provide more efficient getOperand methods. |
| 590 | DECLARE_TRANSPARENT_OPERAND_ACCESSORS(Value); |
Gordon Henriksen | 14a5569 | 2007-12-10 02:14:30 +0000 | [diff] [blame] | 591 | }; |
Robert Bocchino | ca27f03 | 2006-01-17 20:07:22 +0000 | [diff] [blame] | 592 | |
Gordon Henriksen | 14a5569 | 2007-12-10 02:14:30 +0000 | [diff] [blame] | 593 | /// ShuffleVectorConstantExpr - This class is private to |
| 594 | /// Constants.cpp, and is used behind the scenes to implement |
| 595 | /// shufflevector constant exprs. |
| 596 | class VISIBILITY_HIDDEN ShuffleVectorConstantExpr : public ConstantExpr { |
Gabor Greif | e9ecc68 | 2008-04-06 20:25:17 +0000 | [diff] [blame] | 597 | void *operator new(size_t, unsigned); // DO NOT IMPLEMENT |
Gordon Henriksen | 14a5569 | 2007-12-10 02:14:30 +0000 | [diff] [blame] | 598 | public: |
Gabor Greif | e9ecc68 | 2008-04-06 20:25:17 +0000 | [diff] [blame] | 599 | // allocate space for exactly three operands |
| 600 | void *operator new(size_t s) { |
| 601 | return User::operator new(s, 3); |
| 602 | } |
Gordon Henriksen | 14a5569 | 2007-12-10 02:14:30 +0000 | [diff] [blame] | 603 | ShuffleVectorConstantExpr(Constant *C1, Constant *C2, Constant *C3) |
Nate Begeman | 94aa38d | 2009-02-12 21:28:33 +0000 | [diff] [blame] | 604 | : ConstantExpr(VectorType::get( |
| 605 | cast<VectorType>(C1->getType())->getElementType(), |
| 606 | cast<VectorType>(C3->getType())->getNumElements()), |
| 607 | Instruction::ShuffleVector, |
Gabor Greif | f6caff66 | 2008-05-10 08:32:32 +0000 | [diff] [blame] | 608 | &Op<0>(), 3) { |
Gabor Greif | 2d3024d | 2008-05-26 21:33:52 +0000 | [diff] [blame] | 609 | Op<0>() = C1; |
| 610 | Op<1>() = C2; |
| 611 | Op<2>() = C3; |
Gordon Henriksen | 14a5569 | 2007-12-10 02:14:30 +0000 | [diff] [blame] | 612 | } |
Gabor Greif | f6caff66 | 2008-05-10 08:32:32 +0000 | [diff] [blame] | 613 | /// Transparently provide more efficient getOperand methods. |
| 614 | DECLARE_TRANSPARENT_OPERAND_ACCESSORS(Value); |
Gordon Henriksen | 14a5569 | 2007-12-10 02:14:30 +0000 | [diff] [blame] | 615 | }; |
| 616 | |
Dan Gohman | 12fce77 | 2008-05-15 19:50:34 +0000 | [diff] [blame] | 617 | /// ExtractValueConstantExpr - This class is private to |
| 618 | /// Constants.cpp, and is used behind the scenes to implement |
| 619 | /// extractvalue constant exprs. |
| 620 | class VISIBILITY_HIDDEN ExtractValueConstantExpr : public ConstantExpr { |
Dan Gohman | 1ecaf45 | 2008-05-31 00:58:22 +0000 | [diff] [blame] | 621 | void *operator new(size_t, unsigned); // DO NOT IMPLEMENT |
Dan Gohman | 12fce77 | 2008-05-15 19:50:34 +0000 | [diff] [blame] | 622 | public: |
Dan Gohman | 1ecaf45 | 2008-05-31 00:58:22 +0000 | [diff] [blame] | 623 | // allocate space for exactly one operand |
| 624 | void *operator new(size_t s) { |
| 625 | return User::operator new(s, 1); |
Dan Gohman | 12fce77 | 2008-05-15 19:50:34 +0000 | [diff] [blame] | 626 | } |
Dan Gohman | 1ecaf45 | 2008-05-31 00:58:22 +0000 | [diff] [blame] | 627 | ExtractValueConstantExpr(Constant *Agg, |
| 628 | const SmallVector<unsigned, 4> &IdxList, |
| 629 | const Type *DestTy) |
| 630 | : ConstantExpr(DestTy, Instruction::ExtractValue, &Op<0>(), 1), |
| 631 | Indices(IdxList) { |
| 632 | Op<0>() = Agg; |
| 633 | } |
| 634 | |
Dan Gohman | 7bb0450 | 2008-05-31 19:09:08 +0000 | [diff] [blame] | 635 | /// Indices - These identify which value to extract. |
Dan Gohman | 1ecaf45 | 2008-05-31 00:58:22 +0000 | [diff] [blame] | 636 | const SmallVector<unsigned, 4> Indices; |
| 637 | |
Dan Gohman | 12fce77 | 2008-05-15 19:50:34 +0000 | [diff] [blame] | 638 | /// Transparently provide more efficient getOperand methods. |
| 639 | DECLARE_TRANSPARENT_OPERAND_ACCESSORS(Value); |
| 640 | }; |
| 641 | |
| 642 | /// InsertValueConstantExpr - This class is private to |
| 643 | /// Constants.cpp, and is used behind the scenes to implement |
| 644 | /// insertvalue constant exprs. |
| 645 | class VISIBILITY_HIDDEN InsertValueConstantExpr : public ConstantExpr { |
Dan Gohman | 1ecaf45 | 2008-05-31 00:58:22 +0000 | [diff] [blame] | 646 | void *operator new(size_t, unsigned); // DO NOT IMPLEMENT |
Dan Gohman | 12fce77 | 2008-05-15 19:50:34 +0000 | [diff] [blame] | 647 | public: |
Dan Gohman | 1ecaf45 | 2008-05-31 00:58:22 +0000 | [diff] [blame] | 648 | // allocate space for exactly one operand |
| 649 | void *operator new(size_t s) { |
| 650 | return User::operator new(s, 2); |
Dan Gohman | 12fce77 | 2008-05-15 19:50:34 +0000 | [diff] [blame] | 651 | } |
Dan Gohman | 1ecaf45 | 2008-05-31 00:58:22 +0000 | [diff] [blame] | 652 | InsertValueConstantExpr(Constant *Agg, Constant *Val, |
| 653 | const SmallVector<unsigned, 4> &IdxList, |
| 654 | const Type *DestTy) |
| 655 | : ConstantExpr(DestTy, Instruction::InsertValue, &Op<0>(), 2), |
| 656 | Indices(IdxList) { |
| 657 | Op<0>() = Agg; |
| 658 | Op<1>() = Val; |
| 659 | } |
| 660 | |
Dan Gohman | 7bb0450 | 2008-05-31 19:09:08 +0000 | [diff] [blame] | 661 | /// Indices - These identify the position for the insertion. |
Dan Gohman | 1ecaf45 | 2008-05-31 00:58:22 +0000 | [diff] [blame] | 662 | const SmallVector<unsigned, 4> Indices; |
| 663 | |
Dan Gohman | 12fce77 | 2008-05-15 19:50:34 +0000 | [diff] [blame] | 664 | /// Transparently provide more efficient getOperand methods. |
| 665 | DECLARE_TRANSPARENT_OPERAND_ACCESSORS(Value); |
| 666 | }; |
| 667 | |
| 668 | |
Gordon Henriksen | 14a5569 | 2007-12-10 02:14:30 +0000 | [diff] [blame] | 669 | /// GetElementPtrConstantExpr - This class is private to Constants.cpp, and is |
| 670 | /// used behind the scenes to implement getelementpr constant exprs. |
Gabor Greif | e9ecc68 | 2008-04-06 20:25:17 +0000 | [diff] [blame] | 671 | class VISIBILITY_HIDDEN GetElementPtrConstantExpr : public ConstantExpr { |
Gordon Henriksen | 14a5569 | 2007-12-10 02:14:30 +0000 | [diff] [blame] | 672 | GetElementPtrConstantExpr(Constant *C, const std::vector<Constant*> &IdxList, |
Gabor Greif | f6caff66 | 2008-05-10 08:32:32 +0000 | [diff] [blame] | 673 | const Type *DestTy); |
Gabor Greif | e9ecc68 | 2008-04-06 20:25:17 +0000 | [diff] [blame] | 674 | public: |
Gabor Greif | 697e94c | 2008-05-15 10:04:30 +0000 | [diff] [blame] | 675 | static GetElementPtrConstantExpr *Create(Constant *C, |
| 676 | const std::vector<Constant*>&IdxList, |
Gabor Greif | f6caff66 | 2008-05-10 08:32:32 +0000 | [diff] [blame] | 677 | const Type *DestTy) { |
Gabor Greif | 697e94c | 2008-05-15 10:04:30 +0000 | [diff] [blame] | 678 | return new(IdxList.size() + 1) |
| 679 | GetElementPtrConstantExpr(C, IdxList, DestTy); |
Gabor Greif | e9ecc68 | 2008-04-06 20:25:17 +0000 | [diff] [blame] | 680 | } |
Gabor Greif | f6caff66 | 2008-05-10 08:32:32 +0000 | [diff] [blame] | 681 | /// Transparently provide more efficient getOperand methods. |
| 682 | DECLARE_TRANSPARENT_OPERAND_ACCESSORS(Value); |
Gordon Henriksen | 14a5569 | 2007-12-10 02:14:30 +0000 | [diff] [blame] | 683 | }; |
| 684 | |
| 685 | // CompareConstantExpr - This class is private to Constants.cpp, and is used |
| 686 | // behind the scenes to implement ICmp and FCmp constant expressions. This is |
| 687 | // needed in order to store the predicate value for these instructions. |
| 688 | struct VISIBILITY_HIDDEN CompareConstantExpr : public ConstantExpr { |
Gabor Greif | e9ecc68 | 2008-04-06 20:25:17 +0000 | [diff] [blame] | 689 | void *operator new(size_t, unsigned); // DO NOT IMPLEMENT |
| 690 | // allocate space for exactly two operands |
| 691 | void *operator new(size_t s) { |
| 692 | return User::operator new(s, 2); |
| 693 | } |
Gordon Henriksen | 14a5569 | 2007-12-10 02:14:30 +0000 | [diff] [blame] | 694 | unsigned short predicate; |
Nate Begeman | d219570 | 2008-05-12 19:01:56 +0000 | [diff] [blame] | 695 | CompareConstantExpr(const Type *ty, Instruction::OtherOps opc, |
| 696 | unsigned short pred, Constant* LHS, Constant* RHS) |
| 697 | : ConstantExpr(ty, opc, &Op<0>(), 2), predicate(pred) { |
Gabor Greif | 2d3024d | 2008-05-26 21:33:52 +0000 | [diff] [blame] | 698 | Op<0>() = LHS; |
| 699 | Op<1>() = RHS; |
Gordon Henriksen | 14a5569 | 2007-12-10 02:14:30 +0000 | [diff] [blame] | 700 | } |
Gabor Greif | f6caff66 | 2008-05-10 08:32:32 +0000 | [diff] [blame] | 701 | /// Transparently provide more efficient getOperand methods. |
| 702 | DECLARE_TRANSPARENT_OPERAND_ACCESSORS(Value); |
Gordon Henriksen | 14a5569 | 2007-12-10 02:14:30 +0000 | [diff] [blame] | 703 | }; |
| 704 | |
| 705 | } // end anonymous namespace |
| 706 | |
Gabor Greif | f6caff66 | 2008-05-10 08:32:32 +0000 | [diff] [blame] | 707 | template <> |
| 708 | struct OperandTraits<UnaryConstantExpr> : FixedNumOperandTraits<1> { |
| 709 | }; |
| 710 | DEFINE_TRANSPARENT_OPERAND_ACCESSORS(UnaryConstantExpr, Value) |
| 711 | |
| 712 | template <> |
| 713 | struct OperandTraits<BinaryConstantExpr> : FixedNumOperandTraits<2> { |
| 714 | }; |
| 715 | DEFINE_TRANSPARENT_OPERAND_ACCESSORS(BinaryConstantExpr, Value) |
| 716 | |
| 717 | template <> |
| 718 | struct OperandTraits<SelectConstantExpr> : FixedNumOperandTraits<3> { |
| 719 | }; |
| 720 | DEFINE_TRANSPARENT_OPERAND_ACCESSORS(SelectConstantExpr, Value) |
| 721 | |
| 722 | template <> |
| 723 | struct OperandTraits<ExtractElementConstantExpr> : FixedNumOperandTraits<2> { |
| 724 | }; |
| 725 | DEFINE_TRANSPARENT_OPERAND_ACCESSORS(ExtractElementConstantExpr, Value) |
| 726 | |
| 727 | template <> |
| 728 | struct OperandTraits<InsertElementConstantExpr> : FixedNumOperandTraits<3> { |
| 729 | }; |
| 730 | DEFINE_TRANSPARENT_OPERAND_ACCESSORS(InsertElementConstantExpr, Value) |
| 731 | |
| 732 | template <> |
| 733 | struct OperandTraits<ShuffleVectorConstantExpr> : FixedNumOperandTraits<3> { |
| 734 | }; |
| 735 | DEFINE_TRANSPARENT_OPERAND_ACCESSORS(ShuffleVectorConstantExpr, Value) |
| 736 | |
Dan Gohman | 12fce77 | 2008-05-15 19:50:34 +0000 | [diff] [blame] | 737 | template <> |
Dan Gohman | 1ecaf45 | 2008-05-31 00:58:22 +0000 | [diff] [blame] | 738 | struct OperandTraits<ExtractValueConstantExpr> : FixedNumOperandTraits<1> { |
Dan Gohman | 12fce77 | 2008-05-15 19:50:34 +0000 | [diff] [blame] | 739 | }; |
Dan Gohman | 12fce77 | 2008-05-15 19:50:34 +0000 | [diff] [blame] | 740 | DEFINE_TRANSPARENT_OPERAND_ACCESSORS(ExtractValueConstantExpr, Value) |
| 741 | |
| 742 | template <> |
Dan Gohman | 1ecaf45 | 2008-05-31 00:58:22 +0000 | [diff] [blame] | 743 | struct OperandTraits<InsertValueConstantExpr> : FixedNumOperandTraits<2> { |
Dan Gohman | 12fce77 | 2008-05-15 19:50:34 +0000 | [diff] [blame] | 744 | }; |
Dan Gohman | 12fce77 | 2008-05-15 19:50:34 +0000 | [diff] [blame] | 745 | DEFINE_TRANSPARENT_OPERAND_ACCESSORS(InsertValueConstantExpr, Value) |
| 746 | |
Gabor Greif | f6caff66 | 2008-05-10 08:32:32 +0000 | [diff] [blame] | 747 | template <> |
| 748 | struct OperandTraits<GetElementPtrConstantExpr> : VariadicOperandTraits<1> { |
| 749 | }; |
| 750 | |
| 751 | GetElementPtrConstantExpr::GetElementPtrConstantExpr |
| 752 | (Constant *C, |
| 753 | const std::vector<Constant*> &IdxList, |
| 754 | const Type *DestTy) |
| 755 | : ConstantExpr(DestTy, Instruction::GetElementPtr, |
| 756 | OperandTraits<GetElementPtrConstantExpr>::op_end(this) |
| 757 | - (IdxList.size()+1), |
| 758 | IdxList.size()+1) { |
Gabor Greif | 2d3024d | 2008-05-26 21:33:52 +0000 | [diff] [blame] | 759 | OperandList[0] = C; |
Gabor Greif | f6caff66 | 2008-05-10 08:32:32 +0000 | [diff] [blame] | 760 | for (unsigned i = 0, E = IdxList.size(); i != E; ++i) |
Gabor Greif | 2d3024d | 2008-05-26 21:33:52 +0000 | [diff] [blame] | 761 | OperandList[i+1] = IdxList[i]; |
Gabor Greif | f6caff66 | 2008-05-10 08:32:32 +0000 | [diff] [blame] | 762 | } |
| 763 | |
| 764 | DEFINE_TRANSPARENT_OPERAND_ACCESSORS(GetElementPtrConstantExpr, Value) |
| 765 | |
| 766 | |
| 767 | template <> |
| 768 | struct OperandTraits<CompareConstantExpr> : FixedNumOperandTraits<2> { |
| 769 | }; |
| 770 | DEFINE_TRANSPARENT_OPERAND_ACCESSORS(CompareConstantExpr, Value) |
| 771 | |
| 772 | |
| 773 | } // End llvm namespace |
| 774 | |
Reid Spencer | 6c38f0b | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 775 | |
| 776 | // Utility function for determining if a ConstantExpr is a CastOp or not. This |
| 777 | // can't be inline because we don't want to #include Instruction.h into |
| 778 | // Constant.h |
| 779 | bool ConstantExpr::isCast() const { |
| 780 | return Instruction::isCast(getOpcode()); |
| 781 | } |
| 782 | |
Reid Spencer | ee3c991 | 2006-12-04 05:19:50 +0000 | [diff] [blame] | 783 | bool ConstantExpr::isCompare() const { |
Nick Lewycky | a21d3da | 2009-07-08 03:04:38 +0000 | [diff] [blame] | 784 | return getOpcode() == Instruction::ICmp || getOpcode() == Instruction::FCmp; |
Reid Spencer | ee3c991 | 2006-12-04 05:19:50 +0000 | [diff] [blame] | 785 | } |
| 786 | |
Dan Gohman | 1ecaf45 | 2008-05-31 00:58:22 +0000 | [diff] [blame] | 787 | bool ConstantExpr::hasIndices() const { |
| 788 | return getOpcode() == Instruction::ExtractValue || |
| 789 | getOpcode() == Instruction::InsertValue; |
| 790 | } |
| 791 | |
| 792 | const SmallVector<unsigned, 4> &ConstantExpr::getIndices() const { |
| 793 | if (const ExtractValueConstantExpr *EVCE = |
| 794 | dyn_cast<ExtractValueConstantExpr>(this)) |
| 795 | return EVCE->Indices; |
Dan Gohman | a469bdb | 2008-06-23 16:39:44 +0000 | [diff] [blame] | 796 | |
| 797 | return cast<InsertValueConstantExpr>(this)->Indices; |
Dan Gohman | 1ecaf45 | 2008-05-31 00:58:22 +0000 | [diff] [blame] | 798 | } |
| 799 | |
Chris Lattner | 817175f | 2004-03-29 02:37:53 +0000 | [diff] [blame] | 800 | Constant *ConstantExpr::getNot(Constant *C) { |
Dan Gohman | a5b9645 | 2009-06-04 22:49:04 +0000 | [diff] [blame] | 801 | assert(C->getType()->isIntOrIntVector() && |
| 802 | "Cannot NOT a nonintegral value!"); |
Chris Lattner | 817175f | 2004-03-29 02:37:53 +0000 | [diff] [blame] | 803 | return get(Instruction::Xor, C, |
Dale Johannesen | 47a5ef3 | 2008-08-21 21:20:09 +0000 | [diff] [blame] | 804 | Constant::getAllOnesValue(C->getType())); |
Chris Lattner | 817175f | 2004-03-29 02:37:53 +0000 | [diff] [blame] | 805 | } |
Owen Anderson | 0d2de8c | 2009-06-20 00:24:58 +0000 | [diff] [blame] | 806 | Constant *ConstantExpr::getAdd(Constant *C1, Constant *C2) { |
| 807 | return get(Instruction::Add, C1, C2); |
Chris Lattner | 817175f | 2004-03-29 02:37:53 +0000 | [diff] [blame] | 808 | } |
Owen Anderson | 0d2de8c | 2009-06-20 00:24:58 +0000 | [diff] [blame] | 809 | Constant *ConstantExpr::getFAdd(Constant *C1, Constant *C2) { |
| 810 | return get(Instruction::FAdd, C1, C2); |
Dan Gohman | a5b9645 | 2009-06-04 22:49:04 +0000 | [diff] [blame] | 811 | } |
Owen Anderson | 0d2de8c | 2009-06-20 00:24:58 +0000 | [diff] [blame] | 812 | Constant *ConstantExpr::getSub(Constant *C1, Constant *C2) { |
| 813 | return get(Instruction::Sub, C1, C2); |
Chris Lattner | 817175f | 2004-03-29 02:37:53 +0000 | [diff] [blame] | 814 | } |
Owen Anderson | 0d2de8c | 2009-06-20 00:24:58 +0000 | [diff] [blame] | 815 | Constant *ConstantExpr::getFSub(Constant *C1, Constant *C2) { |
| 816 | return get(Instruction::FSub, C1, C2); |
Dan Gohman | a5b9645 | 2009-06-04 22:49:04 +0000 | [diff] [blame] | 817 | } |
Owen Anderson | 0d2de8c | 2009-06-20 00:24:58 +0000 | [diff] [blame] | 818 | Constant *ConstantExpr::getMul(Constant *C1, Constant *C2) { |
| 819 | return get(Instruction::Mul, C1, C2); |
Chris Lattner | 817175f | 2004-03-29 02:37:53 +0000 | [diff] [blame] | 820 | } |
Owen Anderson | 0d2de8c | 2009-06-20 00:24:58 +0000 | [diff] [blame] | 821 | Constant *ConstantExpr::getFMul(Constant *C1, Constant *C2) { |
| 822 | return get(Instruction::FMul, C1, C2); |
Dan Gohman | a5b9645 | 2009-06-04 22:49:04 +0000 | [diff] [blame] | 823 | } |
Owen Anderson | 0d2de8c | 2009-06-20 00:24:58 +0000 | [diff] [blame] | 824 | Constant *ConstantExpr::getUDiv(Constant *C1, Constant *C2) { |
| 825 | return get(Instruction::UDiv, C1, C2); |
Reid Spencer | 7e80b0b | 2006-10-26 06:15:43 +0000 | [diff] [blame] | 826 | } |
Owen Anderson | 0d2de8c | 2009-06-20 00:24:58 +0000 | [diff] [blame] | 827 | Constant *ConstantExpr::getSDiv(Constant *C1, Constant *C2) { |
| 828 | return get(Instruction::SDiv, C1, C2); |
Reid Spencer | 7e80b0b | 2006-10-26 06:15:43 +0000 | [diff] [blame] | 829 | } |
Owen Anderson | 0d2de8c | 2009-06-20 00:24:58 +0000 | [diff] [blame] | 830 | Constant *ConstantExpr::getFDiv(Constant *C1, Constant *C2) { |
| 831 | return get(Instruction::FDiv, C1, C2); |
Chris Lattner | 817175f | 2004-03-29 02:37:53 +0000 | [diff] [blame] | 832 | } |
Owen Anderson | 0d2de8c | 2009-06-20 00:24:58 +0000 | [diff] [blame] | 833 | Constant *ConstantExpr::getURem(Constant *C1, Constant *C2) { |
| 834 | return get(Instruction::URem, C1, C2); |
Reid Spencer | 7eb55b3 | 2006-11-02 01:53:59 +0000 | [diff] [blame] | 835 | } |
Owen Anderson | 0d2de8c | 2009-06-20 00:24:58 +0000 | [diff] [blame] | 836 | Constant *ConstantExpr::getSRem(Constant *C1, Constant *C2) { |
| 837 | return get(Instruction::SRem, C1, C2); |
Reid Spencer | 7eb55b3 | 2006-11-02 01:53:59 +0000 | [diff] [blame] | 838 | } |
Owen Anderson | 0d2de8c | 2009-06-20 00:24:58 +0000 | [diff] [blame] | 839 | Constant *ConstantExpr::getFRem(Constant *C1, Constant *C2) { |
| 840 | return get(Instruction::FRem, C1, C2); |
Chris Lattner | 817175f | 2004-03-29 02:37:53 +0000 | [diff] [blame] | 841 | } |
Owen Anderson | 0d2de8c | 2009-06-20 00:24:58 +0000 | [diff] [blame] | 842 | Constant *ConstantExpr::getAnd(Constant *C1, Constant *C2) { |
| 843 | return get(Instruction::And, C1, C2); |
Chris Lattner | 817175f | 2004-03-29 02:37:53 +0000 | [diff] [blame] | 844 | } |
Owen Anderson | 0d2de8c | 2009-06-20 00:24:58 +0000 | [diff] [blame] | 845 | Constant *ConstantExpr::getOr(Constant *C1, Constant *C2) { |
| 846 | return get(Instruction::Or, C1, C2); |
Chris Lattner | 817175f | 2004-03-29 02:37:53 +0000 | [diff] [blame] | 847 | } |
Owen Anderson | 0d2de8c | 2009-06-20 00:24:58 +0000 | [diff] [blame] | 848 | Constant *ConstantExpr::getXor(Constant *C1, Constant *C2) { |
| 849 | return get(Instruction::Xor, C1, C2); |
Chris Lattner | 817175f | 2004-03-29 02:37:53 +0000 | [diff] [blame] | 850 | } |
Reid Spencer | 10fbf0e | 2006-12-03 05:48:19 +0000 | [diff] [blame] | 851 | unsigned ConstantExpr::getPredicate() const { |
Nate Begeman | d219570 | 2008-05-12 19:01:56 +0000 | [diff] [blame] | 852 | assert(getOpcode() == Instruction::FCmp || |
Nick Lewycky | a21d3da | 2009-07-08 03:04:38 +0000 | [diff] [blame] | 853 | getOpcode() == Instruction::ICmp); |
Chris Lattner | ef65009 | 2007-10-18 16:26:24 +0000 | [diff] [blame] | 854 | return ((const CompareConstantExpr*)this)->predicate; |
Reid Spencer | 10fbf0e | 2006-12-03 05:48:19 +0000 | [diff] [blame] | 855 | } |
Owen Anderson | 0d2de8c | 2009-06-20 00:24:58 +0000 | [diff] [blame] | 856 | Constant *ConstantExpr::getShl(Constant *C1, Constant *C2) { |
| 857 | return get(Instruction::Shl, C1, C2); |
Chris Lattner | 817175f | 2004-03-29 02:37:53 +0000 | [diff] [blame] | 858 | } |
Owen Anderson | 0d2de8c | 2009-06-20 00:24:58 +0000 | [diff] [blame] | 859 | Constant *ConstantExpr::getLShr(Constant *C1, Constant *C2) { |
| 860 | return get(Instruction::LShr, C1, C2); |
Chris Lattner | 817175f | 2004-03-29 02:37:53 +0000 | [diff] [blame] | 861 | } |
Owen Anderson | 0d2de8c | 2009-06-20 00:24:58 +0000 | [diff] [blame] | 862 | Constant *ConstantExpr::getAShr(Constant *C1, Constant *C2) { |
| 863 | return get(Instruction::AShr, C1, C2); |
Chris Lattner | db8bdba | 2004-05-25 05:32:43 +0000 | [diff] [blame] | 864 | } |
Chris Lattner | 60e0dd7 | 2001-10-03 06:12:09 +0000 | [diff] [blame] | 865 | |
Chris Lattner | 7c1018a | 2006-07-14 19:37:40 +0000 | [diff] [blame] | 866 | /// getWithOperandReplaced - Return a constant expression identical to this |
| 867 | /// one, but with the specified operand set to the specified value. |
Reid Spencer | 6c38f0b | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 868 | Constant * |
| 869 | ConstantExpr::getWithOperandReplaced(unsigned OpNo, Constant *Op) const { |
Chris Lattner | 7c1018a | 2006-07-14 19:37:40 +0000 | [diff] [blame] | 870 | assert(OpNo < getNumOperands() && "Operand num is out of range!"); |
| 871 | assert(Op->getType() == getOperand(OpNo)->getType() && |
| 872 | "Replacing operand with value of different type!"); |
Chris Lattner | 22781634 | 2006-07-14 22:20:01 +0000 | [diff] [blame] | 873 | if (getOperand(OpNo) == Op) |
| 874 | return const_cast<ConstantExpr*>(this); |
Chris Lattner | 7c1018a | 2006-07-14 19:37:40 +0000 | [diff] [blame] | 875 | |
Chris Lattner | 22781634 | 2006-07-14 22:20:01 +0000 | [diff] [blame] | 876 | Constant *Op0, *Op1, *Op2; |
Chris Lattner | 7c1018a | 2006-07-14 19:37:40 +0000 | [diff] [blame] | 877 | switch (getOpcode()) { |
Reid Spencer | 6c38f0b | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 878 | case Instruction::Trunc: |
| 879 | case Instruction::ZExt: |
| 880 | case Instruction::SExt: |
| 881 | case Instruction::FPTrunc: |
| 882 | case Instruction::FPExt: |
| 883 | case Instruction::UIToFP: |
| 884 | case Instruction::SIToFP: |
| 885 | case Instruction::FPToUI: |
| 886 | case Instruction::FPToSI: |
| 887 | case Instruction::PtrToInt: |
| 888 | case Instruction::IntToPtr: |
| 889 | case Instruction::BitCast: |
| 890 | return ConstantExpr::getCast(getOpcode(), Op, getType()); |
Chris Lattner | 22781634 | 2006-07-14 22:20:01 +0000 | [diff] [blame] | 891 | case Instruction::Select: |
| 892 | Op0 = (OpNo == 0) ? Op : getOperand(0); |
| 893 | Op1 = (OpNo == 1) ? Op : getOperand(1); |
| 894 | Op2 = (OpNo == 2) ? Op : getOperand(2); |
| 895 | return ConstantExpr::getSelect(Op0, Op1, Op2); |
| 896 | case Instruction::InsertElement: |
| 897 | Op0 = (OpNo == 0) ? Op : getOperand(0); |
| 898 | Op1 = (OpNo == 1) ? Op : getOperand(1); |
| 899 | Op2 = (OpNo == 2) ? Op : getOperand(2); |
| 900 | return ConstantExpr::getInsertElement(Op0, Op1, Op2); |
| 901 | case Instruction::ExtractElement: |
| 902 | Op0 = (OpNo == 0) ? Op : getOperand(0); |
| 903 | Op1 = (OpNo == 1) ? Op : getOperand(1); |
| 904 | return ConstantExpr::getExtractElement(Op0, Op1); |
| 905 | case Instruction::ShuffleVector: |
| 906 | Op0 = (OpNo == 0) ? Op : getOperand(0); |
| 907 | Op1 = (OpNo == 1) ? Op : getOperand(1); |
| 908 | Op2 = (OpNo == 2) ? Op : getOperand(2); |
| 909 | return ConstantExpr::getShuffleVector(Op0, Op1, Op2); |
Chris Lattner | 7c1018a | 2006-07-14 19:37:40 +0000 | [diff] [blame] | 910 | case Instruction::GetElementPtr: { |
Chris Lattner | b5d7030 | 2007-02-19 20:01:23 +0000 | [diff] [blame] | 911 | SmallVector<Constant*, 8> Ops; |
Dan Gohman | 12fce77 | 2008-05-15 19:50:34 +0000 | [diff] [blame] | 912 | Ops.resize(getNumOperands()-1); |
Chris Lattner | 7c1018a | 2006-07-14 19:37:40 +0000 | [diff] [blame] | 913 | for (unsigned i = 1, e = getNumOperands(); i != e; ++i) |
Dan Gohman | 12fce77 | 2008-05-15 19:50:34 +0000 | [diff] [blame] | 914 | Ops[i-1] = getOperand(i); |
Chris Lattner | 7c1018a | 2006-07-14 19:37:40 +0000 | [diff] [blame] | 915 | if (OpNo == 0) |
Chris Lattner | b5d7030 | 2007-02-19 20:01:23 +0000 | [diff] [blame] | 916 | return ConstantExpr::getGetElementPtr(Op, &Ops[0], Ops.size()); |
Chris Lattner | 7c1018a | 2006-07-14 19:37:40 +0000 | [diff] [blame] | 917 | Ops[OpNo-1] = Op; |
Chris Lattner | b5d7030 | 2007-02-19 20:01:23 +0000 | [diff] [blame] | 918 | return ConstantExpr::getGetElementPtr(getOperand(0), &Ops[0], Ops.size()); |
Chris Lattner | 7c1018a | 2006-07-14 19:37:40 +0000 | [diff] [blame] | 919 | } |
Chris Lattner | 7c1018a | 2006-07-14 19:37:40 +0000 | [diff] [blame] | 920 | default: |
| 921 | assert(getNumOperands() == 2 && "Must be binary operator?"); |
Chris Lattner | 22781634 | 2006-07-14 22:20:01 +0000 | [diff] [blame] | 922 | Op0 = (OpNo == 0) ? Op : getOperand(0); |
| 923 | Op1 = (OpNo == 1) ? Op : getOperand(1); |
| 924 | return ConstantExpr::get(getOpcode(), Op0, Op1); |
| 925 | } |
| 926 | } |
| 927 | |
| 928 | /// getWithOperands - This returns the current constant expression with the |
| 929 | /// operands replaced with the specified values. The specified operands must |
| 930 | /// match count and type with the existing ones. |
| 931 | Constant *ConstantExpr:: |
Chris Lattner | b078e28 | 2008-08-20 22:27:40 +0000 | [diff] [blame] | 932 | getWithOperands(Constant* const *Ops, unsigned NumOps) const { |
| 933 | assert(NumOps == getNumOperands() && "Operand count mismatch!"); |
Chris Lattner | 22781634 | 2006-07-14 22:20:01 +0000 | [diff] [blame] | 934 | bool AnyChange = false; |
Chris Lattner | b078e28 | 2008-08-20 22:27:40 +0000 | [diff] [blame] | 935 | for (unsigned i = 0; i != NumOps; ++i) { |
Chris Lattner | 22781634 | 2006-07-14 22:20:01 +0000 | [diff] [blame] | 936 | assert(Ops[i]->getType() == getOperand(i)->getType() && |
| 937 | "Operand type mismatch!"); |
| 938 | AnyChange |= Ops[i] != getOperand(i); |
| 939 | } |
| 940 | if (!AnyChange) // No operands changed, return self. |
| 941 | return const_cast<ConstantExpr*>(this); |
| 942 | |
| 943 | switch (getOpcode()) { |
Reid Spencer | 6c38f0b | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 944 | case Instruction::Trunc: |
| 945 | case Instruction::ZExt: |
| 946 | case Instruction::SExt: |
| 947 | case Instruction::FPTrunc: |
| 948 | case Instruction::FPExt: |
| 949 | case Instruction::UIToFP: |
| 950 | case Instruction::SIToFP: |
| 951 | case Instruction::FPToUI: |
| 952 | case Instruction::FPToSI: |
| 953 | case Instruction::PtrToInt: |
| 954 | case Instruction::IntToPtr: |
| 955 | case Instruction::BitCast: |
| 956 | return ConstantExpr::getCast(getOpcode(), Ops[0], getType()); |
Chris Lattner | 22781634 | 2006-07-14 22:20:01 +0000 | [diff] [blame] | 957 | case Instruction::Select: |
| 958 | return ConstantExpr::getSelect(Ops[0], Ops[1], Ops[2]); |
| 959 | case Instruction::InsertElement: |
| 960 | return ConstantExpr::getInsertElement(Ops[0], Ops[1], Ops[2]); |
| 961 | case Instruction::ExtractElement: |
| 962 | return ConstantExpr::getExtractElement(Ops[0], Ops[1]); |
| 963 | case Instruction::ShuffleVector: |
| 964 | return ConstantExpr::getShuffleVector(Ops[0], Ops[1], Ops[2]); |
Chris Lattner | b5d7030 | 2007-02-19 20:01:23 +0000 | [diff] [blame] | 965 | case Instruction::GetElementPtr: |
Chris Lattner | b078e28 | 2008-08-20 22:27:40 +0000 | [diff] [blame] | 966 | return ConstantExpr::getGetElementPtr(Ops[0], &Ops[1], NumOps-1); |
Reid Spencer | 266e42b | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 967 | case Instruction::ICmp: |
| 968 | case Instruction::FCmp: |
| 969 | return ConstantExpr::getCompare(getPredicate(), Ops[0], Ops[1]); |
Chris Lattner | 22781634 | 2006-07-14 22:20:01 +0000 | [diff] [blame] | 970 | default: |
| 971 | assert(getNumOperands() == 2 && "Must be binary operator?"); |
| 972 | return ConstantExpr::get(getOpcode(), Ops[0], Ops[1]); |
Chris Lattner | 7c1018a | 2006-07-14 19:37:40 +0000 | [diff] [blame] | 973 | } |
| 974 | } |
| 975 | |
Chris Lattner | 2f7c963 | 2001-06-06 20:29:01 +0000 | [diff] [blame] | 976 | |
| 977 | //===----------------------------------------------------------------------===// |
Chris Lattner | 2f7c963 | 2001-06-06 20:29:01 +0000 | [diff] [blame] | 978 | // isValueValidForType implementations |
| 979 | |
Reid Spencer | e733472 | 2006-12-19 01:28:19 +0000 | [diff] [blame] | 980 | bool ConstantInt::isValueValidForType(const Type *Ty, uint64_t Val) { |
Reid Spencer | 7a9c62b | 2007-01-12 07:05:14 +0000 | [diff] [blame] | 981 | unsigned NumBits = cast<IntegerType>(Ty)->getBitWidth(); // assert okay |
Reid Spencer | 7a9c62b | 2007-01-12 07:05:14 +0000 | [diff] [blame] | 982 | if (Ty == Type::Int1Ty) |
| 983 | return Val == 0 || Val == 1; |
Reid Spencer | d7a00d7 | 2007-02-05 23:47:56 +0000 | [diff] [blame] | 984 | if (NumBits >= 64) |
Reid Spencer | 7a9c62b | 2007-01-12 07:05:14 +0000 | [diff] [blame] | 985 | return true; // always true, has to fit in largest type |
| 986 | uint64_t Max = (1ll << NumBits) - 1; |
| 987 | return Val <= Max; |
Reid Spencer | e733472 | 2006-12-19 01:28:19 +0000 | [diff] [blame] | 988 | } |
| 989 | |
Reid Spencer | e0fc4df | 2006-10-20 07:07:24 +0000 | [diff] [blame] | 990 | bool ConstantInt::isValueValidForType(const Type *Ty, int64_t Val) { |
Reid Spencer | 7a9c62b | 2007-01-12 07:05:14 +0000 | [diff] [blame] | 991 | unsigned NumBits = cast<IntegerType>(Ty)->getBitWidth(); // assert okay |
Reid Spencer | 7a9c62b | 2007-01-12 07:05:14 +0000 | [diff] [blame] | 992 | if (Ty == Type::Int1Ty) |
Reid Spencer | a94d394 | 2007-01-19 21:13:56 +0000 | [diff] [blame] | 993 | return Val == 0 || Val == 1 || Val == -1; |
Reid Spencer | d7a00d7 | 2007-02-05 23:47:56 +0000 | [diff] [blame] | 994 | if (NumBits >= 64) |
Reid Spencer | 7a9c62b | 2007-01-12 07:05:14 +0000 | [diff] [blame] | 995 | return true; // always true, has to fit in largest type |
| 996 | int64_t Min = -(1ll << (NumBits-1)); |
| 997 | int64_t Max = (1ll << (NumBits-1)) - 1; |
| 998 | return (Val >= Min && Val <= Max); |
Chris Lattner | 2f7c963 | 2001-06-06 20:29:01 +0000 | [diff] [blame] | 999 | } |
| 1000 | |
Dale Johannesen | d246b2c | 2007-08-30 00:23:21 +0000 | [diff] [blame] | 1001 | bool ConstantFP::isValueValidForType(const Type *Ty, const APFloat& Val) { |
| 1002 | // convert modifies in place, so make a copy. |
| 1003 | APFloat Val2 = APFloat(Val); |
Dale Johannesen | 4f0bd68 | 2008-10-09 23:00:39 +0000 | [diff] [blame] | 1004 | bool losesInfo; |
Chris Lattner | 6b72759 | 2004-06-17 18:19:28 +0000 | [diff] [blame] | 1005 | switch (Ty->getTypeID()) { |
Chris Lattner | 2f7c963 | 2001-06-06 20:29:01 +0000 | [diff] [blame] | 1006 | default: |
| 1007 | return false; // These can't be represented as floating point! |
| 1008 | |
Dale Johannesen | d246b2c | 2007-08-30 00:23:21 +0000 | [diff] [blame] | 1009 | // FIXME rounding mode needs to be more flexible |
Dale Johannesen | 4f0bd68 | 2008-10-09 23:00:39 +0000 | [diff] [blame] | 1010 | case Type::FloatTyID: { |
| 1011 | if (&Val2.getSemantics() == &APFloat::IEEEsingle) |
| 1012 | return true; |
| 1013 | Val2.convert(APFloat::IEEEsingle, APFloat::rmNearestTiesToEven, &losesInfo); |
| 1014 | return !losesInfo; |
| 1015 | } |
| 1016 | case Type::DoubleTyID: { |
| 1017 | if (&Val2.getSemantics() == &APFloat::IEEEsingle || |
| 1018 | &Val2.getSemantics() == &APFloat::IEEEdouble) |
| 1019 | return true; |
| 1020 | Val2.convert(APFloat::IEEEdouble, APFloat::rmNearestTiesToEven, &losesInfo); |
| 1021 | return !losesInfo; |
| 1022 | } |
Dale Johannesen | bdad809 | 2007-08-09 22:51:36 +0000 | [diff] [blame] | 1023 | case Type::X86_FP80TyID: |
Dale Johannesen | 028084e | 2007-09-12 03:30:33 +0000 | [diff] [blame] | 1024 | return &Val2.getSemantics() == &APFloat::IEEEsingle || |
| 1025 | &Val2.getSemantics() == &APFloat::IEEEdouble || |
| 1026 | &Val2.getSemantics() == &APFloat::x87DoubleExtended; |
Dale Johannesen | bdad809 | 2007-08-09 22:51:36 +0000 | [diff] [blame] | 1027 | case Type::FP128TyID: |
Dale Johannesen | 028084e | 2007-09-12 03:30:33 +0000 | [diff] [blame] | 1028 | return &Val2.getSemantics() == &APFloat::IEEEsingle || |
| 1029 | &Val2.getSemantics() == &APFloat::IEEEdouble || |
| 1030 | &Val2.getSemantics() == &APFloat::IEEEquad; |
Dale Johannesen | 007aa37 | 2007-10-11 18:07:22 +0000 | [diff] [blame] | 1031 | case Type::PPC_FP128TyID: |
| 1032 | return &Val2.getSemantics() == &APFloat::IEEEsingle || |
| 1033 | &Val2.getSemantics() == &APFloat::IEEEdouble || |
| 1034 | &Val2.getSemantics() == &APFloat::PPCDoubleDouble; |
Chris Lattner | 2f7c963 | 2001-06-06 20:29:01 +0000 | [diff] [blame] | 1035 | } |
Chris Lattner | aa237256 | 2006-05-24 17:04:05 +0000 | [diff] [blame] | 1036 | } |
Chris Lattner | 9655e54 | 2001-07-20 19:16:02 +0000 | [diff] [blame] | 1037 | |
Chris Lattner | 49d855c | 2001-09-07 16:46:31 +0000 | [diff] [blame] | 1038 | //===----------------------------------------------------------------------===// |
Chris Lattner | 49d855c | 2001-09-07 16:46:31 +0000 | [diff] [blame] | 1039 | // Factory Function Implementation |
| 1040 | |
Gabor Greif | f6caff66 | 2008-05-10 08:32:32 +0000 | [diff] [blame] | 1041 | |
| 1042 | // The number of operands for each ConstantCreator::create method is |
| 1043 | // determined by the ConstantTraits template. |
Chris Lattner | 98fa07b | 2003-05-23 20:03:32 +0000 | [diff] [blame] | 1044 | // ConstantCreator - A class that is used to create constants by |
| 1045 | // ValueMap*. This class should be partially specialized if there is |
| 1046 | // something strange that needs to be done to interface to the ctor for the |
| 1047 | // constant. |
| 1048 | // |
Chris Lattner | 189d19f | 2003-11-21 20:23:48 +0000 | [diff] [blame] | 1049 | namespace llvm { |
Gabor Greif | f6caff66 | 2008-05-10 08:32:32 +0000 | [diff] [blame] | 1050 | template<class ValType> |
| 1051 | struct ConstantTraits; |
| 1052 | |
| 1053 | template<typename T, typename Alloc> |
| 1054 | struct VISIBILITY_HIDDEN ConstantTraits< std::vector<T, Alloc> > { |
| 1055 | static unsigned uses(const std::vector<T, Alloc>& v) { |
| 1056 | return v.size(); |
| 1057 | } |
| 1058 | }; |
| 1059 | |
Chris Lattner | 189d19f | 2003-11-21 20:23:48 +0000 | [diff] [blame] | 1060 | template<class ConstantClass, class TypeClass, class ValType> |
Chris Lattner | 02157b0 | 2006-06-28 21:38:54 +0000 | [diff] [blame] | 1061 | struct VISIBILITY_HIDDEN ConstantCreator { |
Chris Lattner | 189d19f | 2003-11-21 20:23:48 +0000 | [diff] [blame] | 1062 | static ConstantClass *create(const TypeClass *Ty, const ValType &V) { |
Gabor Greif | f6caff66 | 2008-05-10 08:32:32 +0000 | [diff] [blame] | 1063 | return new(ConstantTraits<ValType>::uses(V)) ConstantClass(Ty, V); |
Chris Lattner | 189d19f | 2003-11-21 20:23:48 +0000 | [diff] [blame] | 1064 | } |
| 1065 | }; |
Misha Brukman | b1c9317 | 2005-04-21 23:48:37 +0000 | [diff] [blame] | 1066 | |
Chris Lattner | 189d19f | 2003-11-21 20:23:48 +0000 | [diff] [blame] | 1067 | template<class ConstantClass, class TypeClass> |
Chris Lattner | 02157b0 | 2006-06-28 21:38:54 +0000 | [diff] [blame] | 1068 | struct VISIBILITY_HIDDEN ConvertConstantType { |
Chris Lattner | 189d19f | 2003-11-21 20:23:48 +0000 | [diff] [blame] | 1069 | static void convert(ConstantClass *OldC, const TypeClass *NewTy) { |
Torok Edwin | 69208f0 | 2009-07-12 07:15:17 +0000 | [diff] [blame] | 1070 | LLVM_UNREACHABLE("This type cannot be converted!"); |
Chris Lattner | 189d19f | 2003-11-21 20:23:48 +0000 | [diff] [blame] | 1071 | } |
| 1072 | }; |
Chris Lattner | b50d135 | 2003-10-05 00:17:43 +0000 | [diff] [blame] | 1073 | |
Chris Lattner | 935aa92 | 2005-10-04 17:48:46 +0000 | [diff] [blame] | 1074 | template<class ValType, class TypeClass, class ConstantClass, |
| 1075 | bool HasLargeKey = false /*true for arrays and structs*/ > |
Chris Lattner | 02157b0 | 2006-06-28 21:38:54 +0000 | [diff] [blame] | 1076 | class VISIBILITY_HIDDEN ValueMap : public AbstractTypeUser { |
Chris Lattner | b64419a | 2005-10-03 22:51:37 +0000 | [diff] [blame] | 1077 | public: |
Jim Laskey | c03caef | 2006-07-17 17:38:29 +0000 | [diff] [blame] | 1078 | typedef std::pair<const Type*, ValType> MapKey; |
| 1079 | typedef std::map<MapKey, Constant *> MapTy; |
| 1080 | typedef std::map<Constant*, typename MapTy::iterator> InverseMapTy; |
| 1081 | typedef std::map<const Type*, typename MapTy::iterator> AbstractTypeMapTy; |
Chris Lattner | b64419a | 2005-10-03 22:51:37 +0000 | [diff] [blame] | 1082 | private: |
Chris Lattner | 5bbf60a5 | 2005-10-04 16:52:46 +0000 | [diff] [blame] | 1083 | /// Map - This is the main map from the element descriptor to the Constants. |
| 1084 | /// This is the primary way we avoid creating two of the same shape |
| 1085 | /// constant. |
Chris Lattner | b50d135 | 2003-10-05 00:17:43 +0000 | [diff] [blame] | 1086 | MapTy Map; |
Chris Lattner | 935aa92 | 2005-10-04 17:48:46 +0000 | [diff] [blame] | 1087 | |
| 1088 | /// InverseMap - If "HasLargeKey" is true, this contains an inverse mapping |
| 1089 | /// from the constants to their element in Map. This is important for |
| 1090 | /// removal of constants from the array, which would otherwise have to scan |
| 1091 | /// through the map with very large keys. |
Jim Laskey | c03caef | 2006-07-17 17:38:29 +0000 | [diff] [blame] | 1092 | InverseMapTy InverseMap; |
Chris Lattner | b50d135 | 2003-10-05 00:17:43 +0000 | [diff] [blame] | 1093 | |
Jim Laskey | c03caef | 2006-07-17 17:38:29 +0000 | [diff] [blame] | 1094 | /// AbstractTypeMap - Map for abstract type constants. |
| 1095 | /// |
Chris Lattner | b50d135 | 2003-10-05 00:17:43 +0000 | [diff] [blame] | 1096 | AbstractTypeMapTy AbstractTypeMap; |
Owen Anderson | 0d2de8c | 2009-06-20 00:24:58 +0000 | [diff] [blame] | 1097 | |
| 1098 | /// ValueMapLock - Mutex for this map. |
| 1099 | sys::SmartMutex<true> ValueMapLock; |
Chris Lattner | 99a669b | 2004-11-19 16:39:44 +0000 | [diff] [blame] | 1100 | |
Chris Lattner | 98fa07b | 2003-05-23 20:03:32 +0000 | [diff] [blame] | 1101 | public: |
Owen Anderson | 6179404 | 2009-06-17 20:10:08 +0000 | [diff] [blame] | 1102 | // NOTE: This function is not locked. It is the caller's responsibility |
| 1103 | // to enforce proper synchronization. |
Jim Laskey | c03caef | 2006-07-17 17:38:29 +0000 | [diff] [blame] | 1104 | typename MapTy::iterator map_end() { return Map.end(); } |
Chris Lattner | b64419a | 2005-10-03 22:51:37 +0000 | [diff] [blame] | 1105 | |
| 1106 | /// InsertOrGetItem - Return an iterator for the specified element. |
| 1107 | /// If the element exists in the map, the returned iterator points to the |
| 1108 | /// entry and Exists=true. If not, the iterator points to the newly |
| 1109 | /// inserted entry and returns Exists=false. Newly inserted entries have |
| 1110 | /// I->second == 0, and should be filled in. |
Owen Anderson | 6179404 | 2009-06-17 20:10:08 +0000 | [diff] [blame] | 1111 | /// NOTE: This function is not locked. It is the caller's responsibility |
| 1112 | // to enforce proper synchronization. |
Jim Laskey | c03caef | 2006-07-17 17:38:29 +0000 | [diff] [blame] | 1113 | typename MapTy::iterator InsertOrGetItem(std::pair<MapKey, Constant *> |
| 1114 | &InsertVal, |
Chris Lattner | b64419a | 2005-10-03 22:51:37 +0000 | [diff] [blame] | 1115 | bool &Exists) { |
Jim Laskey | c03caef | 2006-07-17 17:38:29 +0000 | [diff] [blame] | 1116 | std::pair<typename MapTy::iterator, bool> IP = Map.insert(InsertVal); |
Chris Lattner | b64419a | 2005-10-03 22:51:37 +0000 | [diff] [blame] | 1117 | Exists = !IP.second; |
| 1118 | return IP.first; |
| 1119 | } |
Chris Lattner | 5bbf60a5 | 2005-10-04 16:52:46 +0000 | [diff] [blame] | 1120 | |
Chris Lattner | 935aa92 | 2005-10-04 17:48:46 +0000 | [diff] [blame] | 1121 | private: |
Jim Laskey | c03caef | 2006-07-17 17:38:29 +0000 | [diff] [blame] | 1122 | typename MapTy::iterator FindExistingElement(ConstantClass *CP) { |
Chris Lattner | 935aa92 | 2005-10-04 17:48:46 +0000 | [diff] [blame] | 1123 | if (HasLargeKey) { |
Jim Laskey | c03caef | 2006-07-17 17:38:29 +0000 | [diff] [blame] | 1124 | typename InverseMapTy::iterator IMI = InverseMap.find(CP); |
Chris Lattner | 935aa92 | 2005-10-04 17:48:46 +0000 | [diff] [blame] | 1125 | assert(IMI != InverseMap.end() && IMI->second != Map.end() && |
| 1126 | IMI->second->second == CP && |
| 1127 | "InverseMap corrupt!"); |
| 1128 | return IMI->second; |
| 1129 | } |
| 1130 | |
Jim Laskey | c03caef | 2006-07-17 17:38:29 +0000 | [diff] [blame] | 1131 | typename MapTy::iterator I = |
Dan Gohman | e955c48 | 2008-08-05 14:45:15 +0000 | [diff] [blame] | 1132 | Map.find(MapKey(static_cast<const TypeClass*>(CP->getRawType()), |
| 1133 | getValType(CP))); |
Chris Lattner | 5bbf60a5 | 2005-10-04 16:52:46 +0000 | [diff] [blame] | 1134 | if (I == Map.end() || I->second != CP) { |
| 1135 | // FIXME: This should not use a linear scan. If this gets to be a |
| 1136 | // performance problem, someone should look at this. |
| 1137 | for (I = Map.begin(); I != Map.end() && I->second != CP; ++I) |
| 1138 | /* empty */; |
| 1139 | } |
Chris Lattner | 935aa92 | 2005-10-04 17:48:46 +0000 | [diff] [blame] | 1140 | return I; |
| 1141 | } |
Owen Anderson | f89c38c | 2009-06-17 20:43:39 +0000 | [diff] [blame] | 1142 | |
| 1143 | ConstantClass* Create(const TypeClass *Ty, const ValType &V, |
| 1144 | typename MapTy::iterator I) { |
| 1145 | ConstantClass* Result = |
| 1146 | ConstantCreator<ConstantClass,TypeClass,ValType>::create(Ty, V); |
| 1147 | |
| 1148 | assert(Result->getType() == Ty && "Type specified is not correct!"); |
| 1149 | I = Map.insert(I, std::make_pair(MapKey(Ty, V), Result)); |
| 1150 | |
| 1151 | if (HasLargeKey) // Remember the reverse mapping if needed. |
| 1152 | InverseMap.insert(std::make_pair(Result, I)); |
| 1153 | |
| 1154 | // If the type of the constant is abstract, make sure that an entry |
| 1155 | // exists for it in the AbstractTypeMap. |
| 1156 | if (Ty->isAbstract()) { |
| 1157 | typename AbstractTypeMapTy::iterator TI = |
| 1158 | AbstractTypeMap.find(Ty); |
| 1159 | |
| 1160 | if (TI == AbstractTypeMap.end()) { |
| 1161 | // Add ourselves to the ATU list of the type. |
| 1162 | cast<DerivedType>(Ty)->addAbstractTypeUser(this); |
| 1163 | |
| 1164 | AbstractTypeMap.insert(TI, std::make_pair(Ty, I)); |
| 1165 | } |
| 1166 | } |
| 1167 | |
| 1168 | return Result; |
| 1169 | } |
Chris Lattner | 935aa92 | 2005-10-04 17:48:46 +0000 | [diff] [blame] | 1170 | public: |
| 1171 | |
Chris Lattner | b64419a | 2005-10-03 22:51:37 +0000 | [diff] [blame] | 1172 | /// getOrCreate - Return the specified constant from the map, creating it if |
| 1173 | /// necessary. |
Chris Lattner | 98fa07b | 2003-05-23 20:03:32 +0000 | [diff] [blame] | 1174 | ConstantClass *getOrCreate(const TypeClass *Ty, const ValType &V) { |
Owen Anderson | 5c96ef7 | 2009-07-07 18:33:04 +0000 | [diff] [blame] | 1175 | sys::SmartScopedLock<true> Lock(ValueMapLock); |
Owen Anderson | b07dd95 | 2009-06-19 23:16:19 +0000 | [diff] [blame] | 1176 | MapKey Lookup(Ty, V); |
| 1177 | ConstantClass* Result = 0; |
| 1178 | |
| 1179 | typename MapTy::iterator I = Map.find(Lookup); |
| 1180 | // Is it in the map? |
| 1181 | if (I != Map.end()) |
| 1182 | Result = static_cast<ConstantClass *>(I->second); |
| 1183 | |
| 1184 | if (!Result) { |
| 1185 | // If no preexisting value, create one now... |
| 1186 | Result = Create(Ty, V, I); |
| 1187 | } |
| 1188 | |
| 1189 | return Result; |
| 1190 | } |
Misha Brukman | b1c9317 | 2005-04-21 23:48:37 +0000 | [diff] [blame] | 1191 | |
Owen Anderson | 0d2de8c | 2009-06-20 00:24:58 +0000 | [diff] [blame] | 1192 | void remove(ConstantClass *CP) { |
Owen Anderson | 5c96ef7 | 2009-07-07 18:33:04 +0000 | [diff] [blame] | 1193 | sys::SmartScopedLock<true> Lock(ValueMapLock); |
Jim Laskey | c03caef | 2006-07-17 17:38:29 +0000 | [diff] [blame] | 1194 | typename MapTy::iterator I = FindExistingElement(CP); |
Chris Lattner | b50d135 | 2003-10-05 00:17:43 +0000 | [diff] [blame] | 1195 | assert(I != Map.end() && "Constant not found in constant table!"); |
Chris Lattner | 3e650af | 2004-08-04 04:48:01 +0000 | [diff] [blame] | 1196 | assert(I->second == CP && "Didn't find correct element?"); |
Chris Lattner | b50d135 | 2003-10-05 00:17:43 +0000 | [diff] [blame] | 1197 | |
Chris Lattner | 935aa92 | 2005-10-04 17:48:46 +0000 | [diff] [blame] | 1198 | if (HasLargeKey) // Remember the reverse mapping if needed. |
| 1199 | InverseMap.erase(CP); |
| 1200 | |
Chris Lattner | b50d135 | 2003-10-05 00:17:43 +0000 | [diff] [blame] | 1201 | // Now that we found the entry, make sure this isn't the entry that |
| 1202 | // the AbstractTypeMap points to. |
Jim Laskey | c03caef | 2006-07-17 17:38:29 +0000 | [diff] [blame] | 1203 | const TypeClass *Ty = static_cast<const TypeClass *>(I->first.first); |
Chris Lattner | b50d135 | 2003-10-05 00:17:43 +0000 | [diff] [blame] | 1204 | if (Ty->isAbstract()) { |
| 1205 | assert(AbstractTypeMap.count(Ty) && |
| 1206 | "Abstract type not in AbstractTypeMap?"); |
Jim Laskey | c03caef | 2006-07-17 17:38:29 +0000 | [diff] [blame] | 1207 | typename MapTy::iterator &ATMEntryIt = AbstractTypeMap[Ty]; |
Chris Lattner | b50d135 | 2003-10-05 00:17:43 +0000 | [diff] [blame] | 1208 | if (ATMEntryIt == I) { |
| 1209 | // Yes, we are removing the representative entry for this type. |
| 1210 | // See if there are any other entries of the same type. |
Jim Laskey | c03caef | 2006-07-17 17:38:29 +0000 | [diff] [blame] | 1211 | typename MapTy::iterator TmpIt = ATMEntryIt; |
Misha Brukman | b1c9317 | 2005-04-21 23:48:37 +0000 | [diff] [blame] | 1212 | |
Chris Lattner | b50d135 | 2003-10-05 00:17:43 +0000 | [diff] [blame] | 1213 | // First check the entry before this one... |
| 1214 | if (TmpIt != Map.begin()) { |
| 1215 | --TmpIt; |
| 1216 | if (TmpIt->first.first != Ty) // Not the same type, move back... |
| 1217 | ++TmpIt; |
| 1218 | } |
Misha Brukman | b1c9317 | 2005-04-21 23:48:37 +0000 | [diff] [blame] | 1219 | |
Chris Lattner | b50d135 | 2003-10-05 00:17:43 +0000 | [diff] [blame] | 1220 | // If we didn't find the same type, try to move forward... |
| 1221 | if (TmpIt == ATMEntryIt) { |
| 1222 | ++TmpIt; |
| 1223 | if (TmpIt == Map.end() || TmpIt->first.first != Ty) |
| 1224 | --TmpIt; // No entry afterwards with the same type |
| 1225 | } |
| 1226 | |
| 1227 | // If there is another entry in the map of the same abstract type, |
| 1228 | // update the AbstractTypeMap entry now. |
| 1229 | if (TmpIt != ATMEntryIt) { |
| 1230 | ATMEntryIt = TmpIt; |
| 1231 | } else { |
| 1232 | // Otherwise, we are removing the last instance of this type |
| 1233 | // from the table. Remove from the ATM, and from user list. |
| 1234 | cast<DerivedType>(Ty)->removeAbstractTypeUser(this); |
| 1235 | AbstractTypeMap.erase(Ty); |
| 1236 | } |
Chris Lattner | 98fa07b | 2003-05-23 20:03:32 +0000 | [diff] [blame] | 1237 | } |
Chris Lattner | b50d135 | 2003-10-05 00:17:43 +0000 | [diff] [blame] | 1238 | } |
Misha Brukman | b1c9317 | 2005-04-21 23:48:37 +0000 | [diff] [blame] | 1239 | |
Chris Lattner | b50d135 | 2003-10-05 00:17:43 +0000 | [diff] [blame] | 1240 | Map.erase(I); |
| 1241 | } |
| 1242 | |
Chris Lattner | 3b793c6 | 2005-10-04 21:35:50 +0000 | [diff] [blame] | 1243 | |
| 1244 | /// MoveConstantToNewSlot - If we are about to change C to be the element |
| 1245 | /// specified by I, update our internal data structures to reflect this |
| 1246 | /// fact. |
Owen Anderson | 6179404 | 2009-06-17 20:10:08 +0000 | [diff] [blame] | 1247 | /// NOTE: This function is not locked. It is the responsibility of the |
| 1248 | /// caller to enforce proper synchronization if using this method. |
Jim Laskey | c03caef | 2006-07-17 17:38:29 +0000 | [diff] [blame] | 1249 | void MoveConstantToNewSlot(ConstantClass *C, typename MapTy::iterator I) { |
Chris Lattner | 3b793c6 | 2005-10-04 21:35:50 +0000 | [diff] [blame] | 1250 | // First, remove the old location of the specified constant in the map. |
Jim Laskey | c03caef | 2006-07-17 17:38:29 +0000 | [diff] [blame] | 1251 | typename MapTy::iterator OldI = FindExistingElement(C); |
Chris Lattner | 3b793c6 | 2005-10-04 21:35:50 +0000 | [diff] [blame] | 1252 | assert(OldI != Map.end() && "Constant not found in constant table!"); |
| 1253 | assert(OldI->second == C && "Didn't find correct element?"); |
| 1254 | |
| 1255 | // If this constant is the representative element for its abstract type, |
| 1256 | // update the AbstractTypeMap so that the representative element is I. |
| 1257 | if (C->getType()->isAbstract()) { |
| 1258 | typename AbstractTypeMapTy::iterator ATI = |
| 1259 | AbstractTypeMap.find(C->getType()); |
| 1260 | assert(ATI != AbstractTypeMap.end() && |
| 1261 | "Abstract type not in AbstractTypeMap?"); |
| 1262 | if (ATI->second == OldI) |
| 1263 | ATI->second = I; |
| 1264 | } |
| 1265 | |
| 1266 | // Remove the old entry from the map. |
| 1267 | Map.erase(OldI); |
| 1268 | |
| 1269 | // Update the inverse map so that we know that this constant is now |
| 1270 | // located at descriptor I. |
| 1271 | if (HasLargeKey) { |
| 1272 | assert(I->second == C && "Bad inversemap entry!"); |
| 1273 | InverseMap[C] = I; |
| 1274 | } |
| 1275 | } |
| 1276 | |
Chris Lattner | b50d135 | 2003-10-05 00:17:43 +0000 | [diff] [blame] | 1277 | void refineAbstractType(const DerivedType *OldTy, const Type *NewTy) { |
Owen Anderson | 5c96ef7 | 2009-07-07 18:33:04 +0000 | [diff] [blame] | 1278 | sys::SmartScopedLock<true> Lock(ValueMapLock); |
Misha Brukman | b1c9317 | 2005-04-21 23:48:37 +0000 | [diff] [blame] | 1279 | typename AbstractTypeMapTy::iterator I = |
Jim Laskey | c03caef | 2006-07-17 17:38:29 +0000 | [diff] [blame] | 1280 | AbstractTypeMap.find(cast<Type>(OldTy)); |
Chris Lattner | b50d135 | 2003-10-05 00:17:43 +0000 | [diff] [blame] | 1281 | |
| 1282 | assert(I != AbstractTypeMap.end() && |
| 1283 | "Abstract type not in AbstractTypeMap?"); |
| 1284 | |
| 1285 | // Convert a constant at a time until the last one is gone. The last one |
| 1286 | // leaving will remove() itself, causing the AbstractTypeMapEntry to be |
| 1287 | // eliminated eventually. |
| 1288 | do { |
| 1289 | ConvertConstantType<ConstantClass, |
Jim Laskey | c03caef | 2006-07-17 17:38:29 +0000 | [diff] [blame] | 1290 | TypeClass>::convert( |
| 1291 | static_cast<ConstantClass *>(I->second->second), |
Chris Lattner | b50d135 | 2003-10-05 00:17:43 +0000 | [diff] [blame] | 1292 | cast<TypeClass>(NewTy)); |
| 1293 | |
Jim Laskey | c03caef | 2006-07-17 17:38:29 +0000 | [diff] [blame] | 1294 | I = AbstractTypeMap.find(cast<Type>(OldTy)); |
Chris Lattner | b50d135 | 2003-10-05 00:17:43 +0000 | [diff] [blame] | 1295 | } while (I != AbstractTypeMap.end()); |
| 1296 | } |
| 1297 | |
| 1298 | // If the type became concrete without being refined to any other existing |
| 1299 | // type, we just remove ourselves from the ATU list. |
| 1300 | void typeBecameConcrete(const DerivedType *AbsTy) { |
Owen Anderson | d830eb8 | 2009-06-18 19:10:19 +0000 | [diff] [blame] | 1301 | AbsTy->removeAbstractTypeUser(this); |
Chris Lattner | b50d135 | 2003-10-05 00:17:43 +0000 | [diff] [blame] | 1302 | } |
| 1303 | |
| 1304 | void dump() const { |
Bill Wendling | 6a462f1 | 2006-11-17 08:03:48 +0000 | [diff] [blame] | 1305 | DOUT << "Constant.cpp: ValueMap\n"; |
Chris Lattner | 98fa07b | 2003-05-23 20:03:32 +0000 | [diff] [blame] | 1306 | } |
| 1307 | }; |
| 1308 | } |
| 1309 | |
Chris Lattner | a84df0a2 | 2006-09-28 23:36:21 +0000 | [diff] [blame] | 1310 | |
Chris Lattner | 2817350 | 2007-02-20 06:11:36 +0000 | [diff] [blame] | 1311 | |
Chris Lattner | 9fba3da | 2004-02-15 05:53:04 +0000 | [diff] [blame] | 1312 | //---- ConstantAggregateZero::get() implementation... |
| 1313 | // |
| 1314 | namespace llvm { |
| 1315 | // ConstantAggregateZero does not take extra "value" argument... |
| 1316 | template<class ValType> |
| 1317 | struct ConstantCreator<ConstantAggregateZero, Type, ValType> { |
| 1318 | static ConstantAggregateZero *create(const Type *Ty, const ValType &V){ |
| 1319 | return new ConstantAggregateZero(Ty); |
| 1320 | } |
| 1321 | }; |
| 1322 | |
| 1323 | template<> |
| 1324 | struct ConvertConstantType<ConstantAggregateZero, Type> { |
| 1325 | static void convert(ConstantAggregateZero *OldC, const Type *NewTy) { |
| 1326 | // Make everyone now use a constant of the new type... |
Owen Anderson | 0d2de8c | 2009-06-20 00:24:58 +0000 | [diff] [blame] | 1327 | Constant *New = ConstantAggregateZero::get(NewTy); |
Chris Lattner | 9fba3da | 2004-02-15 05:53:04 +0000 | [diff] [blame] | 1328 | assert(New != OldC && "Didn't replace constant??"); |
| 1329 | OldC->uncheckedReplaceAllUsesWith(New); |
Owen Anderson | 0d2de8c | 2009-06-20 00:24:58 +0000 | [diff] [blame] | 1330 | OldC->destroyConstant(); // This constant is now dead, destroy it. |
Chris Lattner | 9fba3da | 2004-02-15 05:53:04 +0000 | [diff] [blame] | 1331 | } |
| 1332 | }; |
| 1333 | } |
| 1334 | |
Chris Lattner | 69edc98 | 2006-09-28 00:35:06 +0000 | [diff] [blame] | 1335 | static ManagedStatic<ValueMap<char, Type, |
| 1336 | ConstantAggregateZero> > AggZeroConstants; |
Chris Lattner | 9fba3da | 2004-02-15 05:53:04 +0000 | [diff] [blame] | 1337 | |
Chris Lattner | 3e650af | 2004-08-04 04:48:01 +0000 | [diff] [blame] | 1338 | static char getValType(ConstantAggregateZero *CPZ) { return 0; } |
| 1339 | |
Owen Anderson | 0d2de8c | 2009-06-20 00:24:58 +0000 | [diff] [blame] | 1340 | ConstantAggregateZero *ConstantAggregateZero::get(const Type *Ty) { |
Reid Spencer | d84d35b | 2007-02-15 02:26:10 +0000 | [diff] [blame] | 1341 | assert((isa<StructType>(Ty) || isa<ArrayType>(Ty) || isa<VectorType>(Ty)) && |
Chris Lattner | bfd0b6d | 2006-06-10 04:16:23 +0000 | [diff] [blame] | 1342 | "Cannot create an aggregate zero of non-aggregate type!"); |
Owen Anderson | 0d2de8c | 2009-06-20 00:24:58 +0000 | [diff] [blame] | 1343 | |
| 1344 | // Implicitly locked. |
| 1345 | return AggZeroConstants->getOrCreate(Ty, 0); |
Chris Lattner | 9fba3da | 2004-02-15 05:53:04 +0000 | [diff] [blame] | 1346 | } |
| 1347 | |
Dan Gohman | 92b551b | 2009-03-03 02:55:14 +0000 | [diff] [blame] | 1348 | /// destroyConstant - Remove the constant from the constant table... |
| 1349 | /// |
Owen Anderson | 0d2de8c | 2009-06-20 00:24:58 +0000 | [diff] [blame] | 1350 | void ConstantAggregateZero::destroyConstant() { |
Owen Anderson | 6179404 | 2009-06-17 20:10:08 +0000 | [diff] [blame] | 1351 | // Implicitly locked. |
Owen Anderson | 0d2de8c | 2009-06-20 00:24:58 +0000 | [diff] [blame] | 1352 | AggZeroConstants->remove(this); |
Chris Lattner | 9fba3da | 2004-02-15 05:53:04 +0000 | [diff] [blame] | 1353 | destroyConstantImpl(); |
| 1354 | } |
| 1355 | |
Chris Lattner | 3462ae3 | 2001-12-03 22:26:30 +0000 | [diff] [blame] | 1356 | //---- ConstantArray::get() implementation... |
Chris Lattner | 49d855c | 2001-09-07 16:46:31 +0000 | [diff] [blame] | 1357 | // |
Chris Lattner | 189d19f | 2003-11-21 20:23:48 +0000 | [diff] [blame] | 1358 | namespace llvm { |
| 1359 | template<> |
| 1360 | struct ConvertConstantType<ConstantArray, ArrayType> { |
| 1361 | static void convert(ConstantArray *OldC, const ArrayType *NewTy) { |
| 1362 | // Make everyone now use a constant of the new type... |
| 1363 | std::vector<Constant*> C; |
| 1364 | for (unsigned i = 0, e = OldC->getNumOperands(); i != e; ++i) |
| 1365 | C.push_back(cast<Constant>(OldC->getOperand(i))); |
Owen Anderson | 0d2de8c | 2009-06-20 00:24:58 +0000 | [diff] [blame] | 1366 | Constant *New = ConstantArray::get(NewTy, C); |
Chris Lattner | 189d19f | 2003-11-21 20:23:48 +0000 | [diff] [blame] | 1367 | assert(New != OldC && "Didn't replace constant??"); |
| 1368 | OldC->uncheckedReplaceAllUsesWith(New); |
Owen Anderson | 0d2de8c | 2009-06-20 00:24:58 +0000 | [diff] [blame] | 1369 | OldC->destroyConstant(); // This constant is now dead, destroy it. |
Chris Lattner | 189d19f | 2003-11-21 20:23:48 +0000 | [diff] [blame] | 1370 | } |
| 1371 | }; |
| 1372 | } |
Chris Lattner | b50d135 | 2003-10-05 00:17:43 +0000 | [diff] [blame] | 1373 | |
Chris Lattner | 3e650af | 2004-08-04 04:48:01 +0000 | [diff] [blame] | 1374 | static std::vector<Constant*> getValType(ConstantArray *CA) { |
| 1375 | std::vector<Constant*> Elements; |
| 1376 | Elements.reserve(CA->getNumOperands()); |
| 1377 | for (unsigned i = 0, e = CA->getNumOperands(); i != e; ++i) |
| 1378 | Elements.push_back(cast<Constant>(CA->getOperand(i))); |
| 1379 | return Elements; |
| 1380 | } |
| 1381 | |
Chris Lattner | b64419a | 2005-10-03 22:51:37 +0000 | [diff] [blame] | 1382 | typedef ValueMap<std::vector<Constant*>, ArrayType, |
Chris Lattner | 935aa92 | 2005-10-04 17:48:46 +0000 | [diff] [blame] | 1383 | ConstantArray, true /*largekey*/> ArrayConstantsTy; |
Chris Lattner | 69edc98 | 2006-09-28 00:35:06 +0000 | [diff] [blame] | 1384 | static ManagedStatic<ArrayConstantsTy> ArrayConstants; |
Chris Lattner | 49d855c | 2001-09-07 16:46:31 +0000 | [diff] [blame] | 1385 | |
Chris Lattner | 015e821 | 2004-02-15 04:14:47 +0000 | [diff] [blame] | 1386 | Constant *ConstantArray::get(const ArrayType *Ty, |
Owen Anderson | 0d2de8c | 2009-06-20 00:24:58 +0000 | [diff] [blame] | 1387 | const std::vector<Constant*> &V) { |
Chris Lattner | 9fba3da | 2004-02-15 05:53:04 +0000 | [diff] [blame] | 1388 | // If this is an all-zero array, return a ConstantAggregateZero object |
| 1389 | if (!V.empty()) { |
| 1390 | Constant *C = V[0]; |
Owen Anderson | 2d7231d | 2009-06-17 18:40:29 +0000 | [diff] [blame] | 1391 | if (!C->isNullValue()) { |
Owen Anderson | 0d2de8c | 2009-06-20 00:24:58 +0000 | [diff] [blame] | 1392 | // Implicitly locked. |
| 1393 | return ArrayConstants->getOrCreate(Ty, V); |
Owen Anderson | 2d7231d | 2009-06-17 18:40:29 +0000 | [diff] [blame] | 1394 | } |
Chris Lattner | 9fba3da | 2004-02-15 05:53:04 +0000 | [diff] [blame] | 1395 | for (unsigned i = 1, e = V.size(); i != e; ++i) |
Owen Anderson | 2d7231d | 2009-06-17 18:40:29 +0000 | [diff] [blame] | 1396 | if (V[i] != C) { |
Owen Anderson | 0d2de8c | 2009-06-20 00:24:58 +0000 | [diff] [blame] | 1397 | // Implicitly locked. |
| 1398 | return ArrayConstants->getOrCreate(Ty, V); |
Owen Anderson | 2d7231d | 2009-06-17 18:40:29 +0000 | [diff] [blame] | 1399 | } |
Chris Lattner | 9fba3da | 2004-02-15 05:53:04 +0000 | [diff] [blame] | 1400 | } |
Owen Anderson | 2d7231d | 2009-06-17 18:40:29 +0000 | [diff] [blame] | 1401 | |
Owen Anderson | 0d2de8c | 2009-06-20 00:24:58 +0000 | [diff] [blame] | 1402 | return ConstantAggregateZero::get(Ty); |
Chris Lattner | 49d855c | 2001-09-07 16:46:31 +0000 | [diff] [blame] | 1403 | } |
| 1404 | |
Dan Gohman | 92b551b | 2009-03-03 02:55:14 +0000 | [diff] [blame] | 1405 | /// destroyConstant - Remove the constant from the constant table... |
| 1406 | /// |
Owen Anderson | 0d2de8c | 2009-06-20 00:24:58 +0000 | [diff] [blame] | 1407 | void ConstantArray::destroyConstant() { |
Owen Anderson | 59ba814 | 2009-06-19 18:34:09 +0000 | [diff] [blame] | 1408 | // Implicitly locked. |
Owen Anderson | 0d2de8c | 2009-06-20 00:24:58 +0000 | [diff] [blame] | 1409 | ArrayConstants->remove(this); |
Chris Lattner | 98fa07b | 2003-05-23 20:03:32 +0000 | [diff] [blame] | 1410 | destroyConstantImpl(); |
| 1411 | } |
| 1412 | |
Reid Spencer | 6f61453 | 2006-05-30 08:23:18 +0000 | [diff] [blame] | 1413 | /// ConstantArray::get(const string&) - Return an array that is initialized to |
| 1414 | /// contain the specified string. If length is zero then a null terminator is |
| 1415 | /// added to the specified string so that it may be used in a natural way. |
| 1416 | /// Otherwise, the length parameter specifies how much of the string to use |
| 1417 | /// and it won't be null terminated. |
| 1418 | /// |
Owen Anderson | 0d2de8c | 2009-06-20 00:24:58 +0000 | [diff] [blame] | 1419 | Constant *ConstantArray::get(const std::string &Str, bool AddNull) { |
Chris Lattner | 7f74a56 | 2002-01-20 22:54:45 +0000 | [diff] [blame] | 1420 | std::vector<Constant*> ElementVals; |
Reid Spencer | 82ebaba | 2006-05-30 18:15:07 +0000 | [diff] [blame] | 1421 | for (unsigned i = 0; i < Str.length(); ++i) |
Reid Spencer | 8d9336d | 2006-12-31 05:26:44 +0000 | [diff] [blame] | 1422 | ElementVals.push_back(ConstantInt::get(Type::Int8Ty, Str[i])); |
Chris Lattner | 8f80fe0 | 2001-10-14 23:54:12 +0000 | [diff] [blame] | 1423 | |
| 1424 | // Add a null terminator to the string... |
Reid Spencer | 82ebaba | 2006-05-30 18:15:07 +0000 | [diff] [blame] | 1425 | if (AddNull) { |
Reid Spencer | 8d9336d | 2006-12-31 05:26:44 +0000 | [diff] [blame] | 1426 | ElementVals.push_back(ConstantInt::get(Type::Int8Ty, 0)); |
Reid Spencer | 6f61453 | 2006-05-30 08:23:18 +0000 | [diff] [blame] | 1427 | } |
Chris Lattner | 8f80fe0 | 2001-10-14 23:54:12 +0000 | [diff] [blame] | 1428 | |
Reid Spencer | 8d9336d | 2006-12-31 05:26:44 +0000 | [diff] [blame] | 1429 | ArrayType *ATy = ArrayType::get(Type::Int8Ty, ElementVals.size()); |
Owen Anderson | 0d2de8c | 2009-06-20 00:24:58 +0000 | [diff] [blame] | 1430 | return ConstantArray::get(ATy, ElementVals); |
Vikram S. Adve | 3441043 | 2001-10-14 23:17:20 +0000 | [diff] [blame] | 1431 | } |
| 1432 | |
Reid Spencer | 2546b76 | 2007-01-26 07:37:34 +0000 | [diff] [blame] | 1433 | /// isString - This method returns true if the array is an array of i8, and |
| 1434 | /// if the elements of the array are all ConstantInt's. |
Chris Lattner | e8dfcca | 2004-01-14 17:06:38 +0000 | [diff] [blame] | 1435 | bool ConstantArray::isString() const { |
Reid Spencer | 2546b76 | 2007-01-26 07:37:34 +0000 | [diff] [blame] | 1436 | // Check the element type for i8... |
Reid Spencer | 8d9336d | 2006-12-31 05:26:44 +0000 | [diff] [blame] | 1437 | if (getType()->getElementType() != Type::Int8Ty) |
Chris Lattner | e8dfcca | 2004-01-14 17:06:38 +0000 | [diff] [blame] | 1438 | return false; |
| 1439 | // Check the elements to make sure they are all integers, not constant |
| 1440 | // expressions. |
| 1441 | for (unsigned i = 0, e = getNumOperands(); i != e; ++i) |
| 1442 | if (!isa<ConstantInt>(getOperand(i))) |
| 1443 | return false; |
| 1444 | return true; |
| 1445 | } |
| 1446 | |
Evan Cheng | 3763c5b | 2006-10-26 19:15:05 +0000 | [diff] [blame] | 1447 | /// isCString - This method returns true if the array is a string (see |
Dan Gohman | 92b551b | 2009-03-03 02:55:14 +0000 | [diff] [blame] | 1448 | /// 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] | 1449 | /// null bytes except its terminator. |
Owen Anderson | 53a5221 | 2009-07-13 04:09:18 +0000 | [diff] [blame^] | 1450 | bool ConstantArray::isCString(LLVMContext &Context) const { |
Reid Spencer | 2546b76 | 2007-01-26 07:37:34 +0000 | [diff] [blame] | 1451 | // Check the element type for i8... |
Reid Spencer | 8d9336d | 2006-12-31 05:26:44 +0000 | [diff] [blame] | 1452 | if (getType()->getElementType() != Type::Int8Ty) |
Evan Cheng | e974da6 | 2006-10-26 21:48:03 +0000 | [diff] [blame] | 1453 | return false; |
Owen Anderson | 53a5221 | 2009-07-13 04:09:18 +0000 | [diff] [blame^] | 1454 | Constant *Zero = Context.getNullValue(getOperand(0)->getType()); |
Evan Cheng | e974da6 | 2006-10-26 21:48:03 +0000 | [diff] [blame] | 1455 | // Last element must be a null. |
| 1456 | if (getOperand(getNumOperands()-1) != Zero) |
| 1457 | return false; |
| 1458 | // Other elements must be non-null integers. |
| 1459 | for (unsigned i = 0, e = getNumOperands()-1; i != e; ++i) { |
| 1460 | if (!isa<ConstantInt>(getOperand(i))) |
Evan Cheng | 3763c5b | 2006-10-26 19:15:05 +0000 | [diff] [blame] | 1461 | return false; |
Evan Cheng | e974da6 | 2006-10-26 21:48:03 +0000 | [diff] [blame] | 1462 | if (getOperand(i) == Zero) |
| 1463 | return false; |
| 1464 | } |
Evan Cheng | 3763c5b | 2006-10-26 19:15:05 +0000 | [diff] [blame] | 1465 | return true; |
| 1466 | } |
| 1467 | |
| 1468 | |
Dan Gohman | 92b551b | 2009-03-03 02:55:14 +0000 | [diff] [blame] | 1469 | /// getAsString - If the sub-element type of this array is i8 |
| 1470 | /// then this method converts the array to an std::string and returns it. |
| 1471 | /// Otherwise, it asserts out. |
| 1472 | /// |
Chris Lattner | 81fabb0 | 2002-08-26 17:53:56 +0000 | [diff] [blame] | 1473 | std::string ConstantArray::getAsString() const { |
Chris Lattner | e8dfcca | 2004-01-14 17:06:38 +0000 | [diff] [blame] | 1474 | assert(isString() && "Not a string!"); |
Chris Lattner | 81fabb0 | 2002-08-26 17:53:56 +0000 | [diff] [blame] | 1475 | std::string Result; |
Owen Anderson | 79c69bc | 2008-06-24 21:58:29 +0000 | [diff] [blame] | 1476 | Result.reserve(getNumOperands()); |
Chris Lattner | 6077c31 | 2003-07-23 15:22:26 +0000 | [diff] [blame] | 1477 | for (unsigned i = 0, e = getNumOperands(); i != e; ++i) |
Owen Anderson | ee9c30d | 2008-06-25 01:05:05 +0000 | [diff] [blame] | 1478 | Result.push_back((char)cast<ConstantInt>(getOperand(i))->getZExtValue()); |
Chris Lattner | 81fabb0 | 2002-08-26 17:53:56 +0000 | [diff] [blame] | 1479 | return Result; |
| 1480 | } |
| 1481 | |
| 1482 | |
Chris Lattner | 3462ae3 | 2001-12-03 22:26:30 +0000 | [diff] [blame] | 1483 | //---- ConstantStruct::get() implementation... |
Chris Lattner | 49d855c | 2001-09-07 16:46:31 +0000 | [diff] [blame] | 1484 | // |
Chris Lattner | b50d135 | 2003-10-05 00:17:43 +0000 | [diff] [blame] | 1485 | |
Chris Lattner | 189d19f | 2003-11-21 20:23:48 +0000 | [diff] [blame] | 1486 | namespace llvm { |
| 1487 | template<> |
| 1488 | struct ConvertConstantType<ConstantStruct, StructType> { |
| 1489 | static void convert(ConstantStruct *OldC, const StructType *NewTy) { |
| 1490 | // Make everyone now use a constant of the new type... |
| 1491 | std::vector<Constant*> C; |
| 1492 | for (unsigned i = 0, e = OldC->getNumOperands(); i != e; ++i) |
| 1493 | C.push_back(cast<Constant>(OldC->getOperand(i))); |
Owen Anderson | 0d2de8c | 2009-06-20 00:24:58 +0000 | [diff] [blame] | 1494 | Constant *New = ConstantStruct::get(NewTy, C); |
Chris Lattner | 189d19f | 2003-11-21 20:23:48 +0000 | [diff] [blame] | 1495 | assert(New != OldC && "Didn't replace constant??"); |
Misha Brukman | b1c9317 | 2005-04-21 23:48:37 +0000 | [diff] [blame] | 1496 | |
Chris Lattner | 189d19f | 2003-11-21 20:23:48 +0000 | [diff] [blame] | 1497 | OldC->uncheckedReplaceAllUsesWith(New); |
Owen Anderson | 0d2de8c | 2009-06-20 00:24:58 +0000 | [diff] [blame] | 1498 | OldC->destroyConstant(); // This constant is now dead, destroy it. |
Chris Lattner | 189d19f | 2003-11-21 20:23:48 +0000 | [diff] [blame] | 1499 | } |
| 1500 | }; |
| 1501 | } |
Chris Lattner | b50d135 | 2003-10-05 00:17:43 +0000 | [diff] [blame] | 1502 | |
Chris Lattner | 8760ec7 | 2005-10-04 01:17:50 +0000 | [diff] [blame] | 1503 | typedef ValueMap<std::vector<Constant*>, StructType, |
Chris Lattner | 935aa92 | 2005-10-04 17:48:46 +0000 | [diff] [blame] | 1504 | ConstantStruct, true /*largekey*/> StructConstantsTy; |
Chris Lattner | 69edc98 | 2006-09-28 00:35:06 +0000 | [diff] [blame] | 1505 | static ManagedStatic<StructConstantsTy> StructConstants; |
Chris Lattner | 49d855c | 2001-09-07 16:46:31 +0000 | [diff] [blame] | 1506 | |
Chris Lattner | 3e650af | 2004-08-04 04:48:01 +0000 | [diff] [blame] | 1507 | static std::vector<Constant*> getValType(ConstantStruct *CS) { |
| 1508 | std::vector<Constant*> Elements; |
| 1509 | Elements.reserve(CS->getNumOperands()); |
| 1510 | for (unsigned i = 0, e = CS->getNumOperands(); i != e; ++i) |
| 1511 | Elements.push_back(cast<Constant>(CS->getOperand(i))); |
| 1512 | return Elements; |
| 1513 | } |
| 1514 | |
Chris Lattner | 015e821 | 2004-02-15 04:14:47 +0000 | [diff] [blame] | 1515 | Constant *ConstantStruct::get(const StructType *Ty, |
Owen Anderson | 0d2de8c | 2009-06-20 00:24:58 +0000 | [diff] [blame] | 1516 | const std::vector<Constant*> &V) { |
Chris Lattner | 9fba3da | 2004-02-15 05:53:04 +0000 | [diff] [blame] | 1517 | // Create a ConstantAggregateZero value if all elements are zeros... |
| 1518 | for (unsigned i = 0, e = V.size(); i != e; ++i) |
Owen Anderson | 6179404 | 2009-06-17 20:10:08 +0000 | [diff] [blame] | 1519 | if (!V[i]->isNullValue()) |
Owen Anderson | 0d2de8c | 2009-06-20 00:24:58 +0000 | [diff] [blame] | 1520 | // Implicitly locked. |
| 1521 | return StructConstants->getOrCreate(Ty, V); |
Chris Lattner | 9fba3da | 2004-02-15 05:53:04 +0000 | [diff] [blame] | 1522 | |
Owen Anderson | 0d2de8c | 2009-06-20 00:24:58 +0000 | [diff] [blame] | 1523 | return ConstantAggregateZero::get(Ty); |
Chris Lattner | 49d855c | 2001-09-07 16:46:31 +0000 | [diff] [blame] | 1524 | } |
Chris Lattner | 883ad0b | 2001-10-03 15:39:36 +0000 | [diff] [blame] | 1525 | |
Owen Anderson | 0d2de8c | 2009-06-20 00:24:58 +0000 | [diff] [blame] | 1526 | Constant *ConstantStruct::get(const std::vector<Constant*> &V, bool packed) { |
Chris Lattner | d6108ca | 2004-07-12 20:35:11 +0000 | [diff] [blame] | 1527 | std::vector<const Type*> StructEls; |
| 1528 | StructEls.reserve(V.size()); |
| 1529 | for (unsigned i = 0, e = V.size(); i != e; ++i) |
| 1530 | StructEls.push_back(V[i]->getType()); |
Owen Anderson | 0d2de8c | 2009-06-20 00:24:58 +0000 | [diff] [blame] | 1531 | return get(StructType::get(StructEls, packed), V); |
Chris Lattner | d6108ca | 2004-07-12 20:35:11 +0000 | [diff] [blame] | 1532 | } |
| 1533 | |
Chris Lattner | d7a7330 | 2001-10-13 06:57:33 +0000 | [diff] [blame] | 1534 | // destroyConstant - Remove the constant from the constant table... |
Chris Lattner | 883ad0b | 2001-10-03 15:39:36 +0000 | [diff] [blame] | 1535 | // |
Owen Anderson | 0d2de8c | 2009-06-20 00:24:58 +0000 | [diff] [blame] | 1536 | void ConstantStruct::destroyConstant() { |
Owen Anderson | 59ba814 | 2009-06-19 18:34:09 +0000 | [diff] [blame] | 1537 | // Implicitly locked. |
Owen Anderson | 0d2de8c | 2009-06-20 00:24:58 +0000 | [diff] [blame] | 1538 | StructConstants->remove(this); |
Chris Lattner | d7a7330 | 2001-10-13 06:57:33 +0000 | [diff] [blame] | 1539 | destroyConstantImpl(); |
| 1540 | } |
Chris Lattner | 883ad0b | 2001-10-03 15:39:36 +0000 | [diff] [blame] | 1541 | |
Reid Spencer | d84d35b | 2007-02-15 02:26:10 +0000 | [diff] [blame] | 1542 | //---- ConstantVector::get() implementation... |
Brian Gaeke | 0220904 | 2004-08-20 06:00:58 +0000 | [diff] [blame] | 1543 | // |
| 1544 | namespace llvm { |
| 1545 | template<> |
Reid Spencer | d84d35b | 2007-02-15 02:26:10 +0000 | [diff] [blame] | 1546 | struct ConvertConstantType<ConstantVector, VectorType> { |
| 1547 | static void convert(ConstantVector *OldC, const VectorType *NewTy) { |
Brian Gaeke | 0220904 | 2004-08-20 06:00:58 +0000 | [diff] [blame] | 1548 | // Make everyone now use a constant of the new type... |
| 1549 | std::vector<Constant*> C; |
| 1550 | for (unsigned i = 0, e = OldC->getNumOperands(); i != e; ++i) |
| 1551 | C.push_back(cast<Constant>(OldC->getOperand(i))); |
Owen Anderson | 0d2de8c | 2009-06-20 00:24:58 +0000 | [diff] [blame] | 1552 | Constant *New = ConstantVector::get(NewTy, C); |
Brian Gaeke | 0220904 | 2004-08-20 06:00:58 +0000 | [diff] [blame] | 1553 | assert(New != OldC && "Didn't replace constant??"); |
| 1554 | OldC->uncheckedReplaceAllUsesWith(New); |
Owen Anderson | 0d2de8c | 2009-06-20 00:24:58 +0000 | [diff] [blame] | 1555 | OldC->destroyConstant(); // This constant is now dead, destroy it. |
Brian Gaeke | 0220904 | 2004-08-20 06:00:58 +0000 | [diff] [blame] | 1556 | } |
| 1557 | }; |
| 1558 | } |
| 1559 | |
Reid Spencer | d84d35b | 2007-02-15 02:26:10 +0000 | [diff] [blame] | 1560 | static std::vector<Constant*> getValType(ConstantVector *CP) { |
Brian Gaeke | 0220904 | 2004-08-20 06:00:58 +0000 | [diff] [blame] | 1561 | std::vector<Constant*> Elements; |
| 1562 | Elements.reserve(CP->getNumOperands()); |
| 1563 | for (unsigned i = 0, e = CP->getNumOperands(); i != e; ++i) |
| 1564 | Elements.push_back(CP->getOperand(i)); |
| 1565 | return Elements; |
| 1566 | } |
| 1567 | |
Reid Spencer | d84d35b | 2007-02-15 02:26:10 +0000 | [diff] [blame] | 1568 | static ManagedStatic<ValueMap<std::vector<Constant*>, VectorType, |
Reid Spencer | 09575ba | 2007-02-15 03:39:18 +0000 | [diff] [blame] | 1569 | ConstantVector> > VectorConstants; |
Brian Gaeke | 0220904 | 2004-08-20 06:00:58 +0000 | [diff] [blame] | 1570 | |
Reid Spencer | d84d35b | 2007-02-15 02:26:10 +0000 | [diff] [blame] | 1571 | Constant *ConstantVector::get(const VectorType *Ty, |
Owen Anderson | 0d2de8c | 2009-06-20 00:24:58 +0000 | [diff] [blame] | 1572 | const std::vector<Constant*> &V) { |
Chris Lattner | d977c07 | 2008-07-10 00:44:03 +0000 | [diff] [blame] | 1573 | assert(!V.empty() && "Vectors can't be empty"); |
| 1574 | // If this is an all-undef or alll-zero vector, return a |
| 1575 | // ConstantAggregateZero or UndefValue. |
| 1576 | Constant *C = V[0]; |
| 1577 | bool isZero = C->isNullValue(); |
| 1578 | bool isUndef = isa<UndefValue>(C); |
| 1579 | |
| 1580 | if (isZero || isUndef) { |
Brian Gaeke | 0220904 | 2004-08-20 06:00:58 +0000 | [diff] [blame] | 1581 | for (unsigned i = 1, e = V.size(); i != e; ++i) |
Chris Lattner | d977c07 | 2008-07-10 00:44:03 +0000 | [diff] [blame] | 1582 | if (V[i] != C) { |
| 1583 | isZero = isUndef = false; |
| 1584 | break; |
| 1585 | } |
Brian Gaeke | 0220904 | 2004-08-20 06:00:58 +0000 | [diff] [blame] | 1586 | } |
Chris Lattner | d977c07 | 2008-07-10 00:44:03 +0000 | [diff] [blame] | 1587 | |
| 1588 | if (isZero) |
Owen Anderson | 0d2de8c | 2009-06-20 00:24:58 +0000 | [diff] [blame] | 1589 | return ConstantAggregateZero::get(Ty); |
Chris Lattner | d977c07 | 2008-07-10 00:44:03 +0000 | [diff] [blame] | 1590 | if (isUndef) |
Owen Anderson | 0d2de8c | 2009-06-20 00:24:58 +0000 | [diff] [blame] | 1591 | return UndefValue::get(Ty); |
Owen Anderson | 6179404 | 2009-06-17 20:10:08 +0000 | [diff] [blame] | 1592 | |
Owen Anderson | 0d2de8c | 2009-06-20 00:24:58 +0000 | [diff] [blame] | 1593 | // Implicitly locked. |
| 1594 | return VectorConstants->getOrCreate(Ty, V); |
Brian Gaeke | 0220904 | 2004-08-20 06:00:58 +0000 | [diff] [blame] | 1595 | } |
| 1596 | |
Owen Anderson | 0d2de8c | 2009-06-20 00:24:58 +0000 | [diff] [blame] | 1597 | Constant *ConstantVector::get(const std::vector<Constant*> &V) { |
Brian Gaeke | 0220904 | 2004-08-20 06:00:58 +0000 | [diff] [blame] | 1598 | assert(!V.empty() && "Cannot infer type if V is empty"); |
Owen Anderson | 0d2de8c | 2009-06-20 00:24:58 +0000 | [diff] [blame] | 1599 | return get(VectorType::get(V.front()->getType(),V.size()), V); |
Brian Gaeke | 0220904 | 2004-08-20 06:00:58 +0000 | [diff] [blame] | 1600 | } |
| 1601 | |
| 1602 | // destroyConstant - Remove the constant from the constant table... |
| 1603 | // |
Owen Anderson | 0d2de8c | 2009-06-20 00:24:58 +0000 | [diff] [blame] | 1604 | void ConstantVector::destroyConstant() { |
Owen Anderson | 59ba814 | 2009-06-19 18:34:09 +0000 | [diff] [blame] | 1605 | // Implicitly locked. |
Owen Anderson | 0d2de8c | 2009-06-20 00:24:58 +0000 | [diff] [blame] | 1606 | VectorConstants->remove(this); |
Brian Gaeke | 0220904 | 2004-08-20 06:00:58 +0000 | [diff] [blame] | 1607 | destroyConstantImpl(); |
| 1608 | } |
| 1609 | |
Dan Gohman | 3097807 | 2007-05-24 14:36:04 +0000 | [diff] [blame] | 1610 | /// This function will return true iff every element in this vector constant |
Jim Laskey | f047882 | 2007-01-12 22:39:14 +0000 | [diff] [blame] | 1611 | /// is set to all ones. |
| 1612 | /// @returns true iff this constant's emements are all set to all ones. |
| 1613 | /// @brief Determine if the value is all ones. |
Reid Spencer | d84d35b | 2007-02-15 02:26:10 +0000 | [diff] [blame] | 1614 | bool ConstantVector::isAllOnesValue() const { |
Jim Laskey | f047882 | 2007-01-12 22:39:14 +0000 | [diff] [blame] | 1615 | // Check out first element. |
| 1616 | const Constant *Elt = getOperand(0); |
| 1617 | const ConstantInt *CI = dyn_cast<ConstantInt>(Elt); |
| 1618 | if (!CI || !CI->isAllOnesValue()) return false; |
| 1619 | // Then make sure all remaining elements point to the same value. |
| 1620 | for (unsigned I = 1, E = getNumOperands(); I < E; ++I) { |
| 1621 | if (getOperand(I) != Elt) return false; |
| 1622 | } |
| 1623 | return true; |
| 1624 | } |
| 1625 | |
Dan Gohman | 0715920 | 2007-10-17 17:51:30 +0000 | [diff] [blame] | 1626 | /// getSplatValue - If this is a splat constant, where all of the |
| 1627 | /// elements have the same value, return that value. Otherwise return null. |
| 1628 | Constant *ConstantVector::getSplatValue() { |
| 1629 | // Check out first element. |
| 1630 | Constant *Elt = getOperand(0); |
| 1631 | // Then make sure all remaining elements point to the same value. |
| 1632 | for (unsigned I = 1, E = getNumOperands(); I < E; ++I) |
| 1633 | if (getOperand(I) != Elt) return 0; |
| 1634 | return Elt; |
| 1635 | } |
| 1636 | |
Chris Lattner | 3462ae3 | 2001-12-03 22:26:30 +0000 | [diff] [blame] | 1637 | //---- ConstantPointerNull::get() implementation... |
Chris Lattner | d7a7330 | 2001-10-13 06:57:33 +0000 | [diff] [blame] | 1638 | // |
Chris Lattner | 98fa07b | 2003-05-23 20:03:32 +0000 | [diff] [blame] | 1639 | |
Chris Lattner | 189d19f | 2003-11-21 20:23:48 +0000 | [diff] [blame] | 1640 | namespace llvm { |
| 1641 | // ConstantPointerNull does not take extra "value" argument... |
| 1642 | template<class ValType> |
| 1643 | struct ConstantCreator<ConstantPointerNull, PointerType, ValType> { |
| 1644 | static ConstantPointerNull *create(const PointerType *Ty, const ValType &V){ |
| 1645 | return new ConstantPointerNull(Ty); |
| 1646 | } |
| 1647 | }; |
Chris Lattner | 98fa07b | 2003-05-23 20:03:32 +0000 | [diff] [blame] | 1648 | |
Chris Lattner | 189d19f | 2003-11-21 20:23:48 +0000 | [diff] [blame] | 1649 | template<> |
| 1650 | struct ConvertConstantType<ConstantPointerNull, PointerType> { |
| 1651 | static void convert(ConstantPointerNull *OldC, const PointerType *NewTy) { |
| 1652 | // Make everyone now use a constant of the new type... |
Owen Anderson | 0d2de8c | 2009-06-20 00:24:58 +0000 | [diff] [blame] | 1653 | Constant *New = ConstantPointerNull::get(NewTy); |
Chris Lattner | 189d19f | 2003-11-21 20:23:48 +0000 | [diff] [blame] | 1654 | assert(New != OldC && "Didn't replace constant??"); |
| 1655 | OldC->uncheckedReplaceAllUsesWith(New); |
Owen Anderson | 0d2de8c | 2009-06-20 00:24:58 +0000 | [diff] [blame] | 1656 | OldC->destroyConstant(); // This constant is now dead, destroy it. |
Chris Lattner | 189d19f | 2003-11-21 20:23:48 +0000 | [diff] [blame] | 1657 | } |
| 1658 | }; |
| 1659 | } |
Chris Lattner | b50d135 | 2003-10-05 00:17:43 +0000 | [diff] [blame] | 1660 | |
Chris Lattner | 69edc98 | 2006-09-28 00:35:06 +0000 | [diff] [blame] | 1661 | static ManagedStatic<ValueMap<char, PointerType, |
| 1662 | ConstantPointerNull> > NullPtrConstants; |
Chris Lattner | d7a7330 | 2001-10-13 06:57:33 +0000 | [diff] [blame] | 1663 | |
Chris Lattner | 3e650af | 2004-08-04 04:48:01 +0000 | [diff] [blame] | 1664 | static char getValType(ConstantPointerNull *) { |
| 1665 | return 0; |
| 1666 | } |
| 1667 | |
| 1668 | |
Owen Anderson | 0d2de8c | 2009-06-20 00:24:58 +0000 | [diff] [blame] | 1669 | ConstantPointerNull *ConstantPointerNull::get(const PointerType *Ty) { |
Owen Anderson | 6179404 | 2009-06-17 20:10:08 +0000 | [diff] [blame] | 1670 | // Implicitly locked. |
Owen Anderson | 0d2de8c | 2009-06-20 00:24:58 +0000 | [diff] [blame] | 1671 | return NullPtrConstants->getOrCreate(Ty, 0); |
Chris Lattner | 883ad0b | 2001-10-03 15:39:36 +0000 | [diff] [blame] | 1672 | } |
| 1673 | |
Chris Lattner | 0c6e0b9 | 2002-08-18 00:40:04 +0000 | [diff] [blame] | 1674 | // destroyConstant - Remove the constant from the constant table... |
| 1675 | // |
Owen Anderson | 0d2de8c | 2009-06-20 00:24:58 +0000 | [diff] [blame] | 1676 | void ConstantPointerNull::destroyConstant() { |
Owen Anderson | 59ba814 | 2009-06-19 18:34:09 +0000 | [diff] [blame] | 1677 | // Implicitly locked. |
Owen Anderson | 0d2de8c | 2009-06-20 00:24:58 +0000 | [diff] [blame] | 1678 | NullPtrConstants->remove(this); |
Chris Lattner | 0c6e0b9 | 2002-08-18 00:40:04 +0000 | [diff] [blame] | 1679 | destroyConstantImpl(); |
| 1680 | } |
| 1681 | |
| 1682 | |
Chris Lattner | d5f67d8 | 2004-10-16 18:07:16 +0000 | [diff] [blame] | 1683 | //---- UndefValue::get() implementation... |
| 1684 | // |
| 1685 | |
| 1686 | namespace llvm { |
| 1687 | // UndefValue does not take extra "value" argument... |
| 1688 | template<class ValType> |
| 1689 | struct ConstantCreator<UndefValue, Type, ValType> { |
| 1690 | static UndefValue *create(const Type *Ty, const ValType &V) { |
| 1691 | return new UndefValue(Ty); |
| 1692 | } |
| 1693 | }; |
| 1694 | |
| 1695 | template<> |
| 1696 | struct ConvertConstantType<UndefValue, Type> { |
| 1697 | static void convert(UndefValue *OldC, const Type *NewTy) { |
| 1698 | // Make everyone now use a constant of the new type. |
Owen Anderson | 0d2de8c | 2009-06-20 00:24:58 +0000 | [diff] [blame] | 1699 | Constant *New = UndefValue::get(NewTy); |
Chris Lattner | d5f67d8 | 2004-10-16 18:07:16 +0000 | [diff] [blame] | 1700 | assert(New != OldC && "Didn't replace constant??"); |
| 1701 | OldC->uncheckedReplaceAllUsesWith(New); |
Owen Anderson | 0d2de8c | 2009-06-20 00:24:58 +0000 | [diff] [blame] | 1702 | OldC->destroyConstant(); // This constant is now dead, destroy it. |
Chris Lattner | d5f67d8 | 2004-10-16 18:07:16 +0000 | [diff] [blame] | 1703 | } |
| 1704 | }; |
| 1705 | } |
| 1706 | |
Chris Lattner | 69edc98 | 2006-09-28 00:35:06 +0000 | [diff] [blame] | 1707 | static ManagedStatic<ValueMap<char, Type, UndefValue> > UndefValueConstants; |
Chris Lattner | d5f67d8 | 2004-10-16 18:07:16 +0000 | [diff] [blame] | 1708 | |
| 1709 | static char getValType(UndefValue *) { |
| 1710 | return 0; |
| 1711 | } |
| 1712 | |
| 1713 | |
Owen Anderson | 0d2de8c | 2009-06-20 00:24:58 +0000 | [diff] [blame] | 1714 | UndefValue *UndefValue::get(const Type *Ty) { |
| 1715 | // Implicitly locked. |
| 1716 | return UndefValueConstants->getOrCreate(Ty, 0); |
Chris Lattner | d5f67d8 | 2004-10-16 18:07:16 +0000 | [diff] [blame] | 1717 | } |
| 1718 | |
| 1719 | // destroyConstant - Remove the constant from the constant table. |
| 1720 | // |
Owen Anderson | 0d2de8c | 2009-06-20 00:24:58 +0000 | [diff] [blame] | 1721 | void UndefValue::destroyConstant() { |
Owen Anderson | 6179404 | 2009-06-17 20:10:08 +0000 | [diff] [blame] | 1722 | // Implicitly locked. |
Owen Anderson | 0d2de8c | 2009-06-20 00:24:58 +0000 | [diff] [blame] | 1723 | UndefValueConstants->remove(this); |
Chris Lattner | d5f67d8 | 2004-10-16 18:07:16 +0000 | [diff] [blame] | 1724 | destroyConstantImpl(); |
| 1725 | } |
| 1726 | |
Nick Lewycky | 49f8919 | 2009-04-04 07:22:01 +0000 | [diff] [blame] | 1727 | //---- MDString::get() implementation |
| 1728 | // |
| 1729 | |
| 1730 | MDString::MDString(const char *begin, const char *end) |
Nick Lewycky | adbc284 | 2009-05-30 05:06:04 +0000 | [diff] [blame] | 1731 | : Constant(Type::MetadataTy, MDStringVal, 0, 0), |
Nick Lewycky | 49f8919 | 2009-04-04 07:22:01 +0000 | [diff] [blame] | 1732 | StrBegin(begin), StrEnd(end) {} |
| 1733 | |
| 1734 | static ManagedStatic<StringMap<MDString*> > MDStringCache; |
| 1735 | |
Owen Anderson | 0d2de8c | 2009-06-20 00:24:58 +0000 | [diff] [blame] | 1736 | MDString *MDString::get(const char *StrBegin, const char *StrEnd) { |
Owen Anderson | 5c96ef7 | 2009-07-07 18:33:04 +0000 | [diff] [blame] | 1737 | sys::SmartScopedWriter<true> Writer(*ConstantsLock); |
Owen Anderson | d830eb8 | 2009-06-18 19:10:19 +0000 | [diff] [blame] | 1738 | StringMapEntry<MDString *> &Entry = MDStringCache->GetOrCreateValue( |
| 1739 | StrBegin, StrEnd); |
| 1740 | MDString *&S = Entry.getValue(); |
| 1741 | if (!S) S = new MDString(Entry.getKeyData(), |
| 1742 | Entry.getKeyData() + Entry.getKeyLength()); |
Owen Anderson | 65c5cd7 | 2009-06-17 20:34:43 +0000 | [diff] [blame] | 1743 | |
Owen Anderson | d830eb8 | 2009-06-18 19:10:19 +0000 | [diff] [blame] | 1744 | return S; |
Nick Lewycky | 49f8919 | 2009-04-04 07:22:01 +0000 | [diff] [blame] | 1745 | } |
| 1746 | |
Devang Patel | 4c56316 | 2009-06-24 22:42:39 +0000 | [diff] [blame] | 1747 | MDString *MDString::get(const std::string &Str) { |
Owen Anderson | 5c96ef7 | 2009-07-07 18:33:04 +0000 | [diff] [blame] | 1748 | sys::SmartScopedWriter<true> Writer(*ConstantsLock); |
Devang Patel | 4c56316 | 2009-06-24 22:42:39 +0000 | [diff] [blame] | 1749 | StringMapEntry<MDString *> &Entry = MDStringCache->GetOrCreateValue( |
| 1750 | Str.data(), Str.data() + Str.size()); |
| 1751 | MDString *&S = Entry.getValue(); |
| 1752 | if (!S) S = new MDString(Entry.getKeyData(), |
| 1753 | Entry.getKeyData() + Entry.getKeyLength()); |
| 1754 | |
| 1755 | return S; |
| 1756 | } |
| 1757 | |
Owen Anderson | 0d2de8c | 2009-06-20 00:24:58 +0000 | [diff] [blame] | 1758 | void MDString::destroyConstant() { |
Owen Anderson | 5c96ef7 | 2009-07-07 18:33:04 +0000 | [diff] [blame] | 1759 | sys::SmartScopedWriter<true> Writer(*ConstantsLock); |
Owen Anderson | d830eb8 | 2009-06-18 19:10:19 +0000 | [diff] [blame] | 1760 | MDStringCache->erase(MDStringCache->find(StrBegin, StrEnd)); |
Nick Lewycky | 49f8919 | 2009-04-04 07:22:01 +0000 | [diff] [blame] | 1761 | destroyConstantImpl(); |
| 1762 | } |
| 1763 | |
| 1764 | //---- MDNode::get() implementation |
| 1765 | // |
| 1766 | |
| 1767 | static ManagedStatic<FoldingSet<MDNode> > MDNodeSet; |
| 1768 | |
Nick Lewycky | b8f9b7a | 2009-05-10 20:57:05 +0000 | [diff] [blame] | 1769 | MDNode::MDNode(Value*const* Vals, unsigned NumVals) |
Nick Lewycky | adbc284 | 2009-05-30 05:06:04 +0000 | [diff] [blame] | 1770 | : Constant(Type::MetadataTy, MDNodeVal, 0, 0) { |
Nick Lewycky | b8f9b7a | 2009-05-10 20:57:05 +0000 | [diff] [blame] | 1771 | for (unsigned i = 0; i != NumVals; ++i) |
| 1772 | Node.push_back(ElementVH(Vals[i], this)); |
Nick Lewycky | 49f8919 | 2009-04-04 07:22:01 +0000 | [diff] [blame] | 1773 | } |
| 1774 | |
Nick Lewycky | b8f9b7a | 2009-05-10 20:57:05 +0000 | [diff] [blame] | 1775 | void MDNode::Profile(FoldingSetNodeID &ID) const { |
| 1776 | for (const_elem_iterator I = elem_begin(), E = elem_end(); I != E; ++I) |
Nick Lewycky | 49f8919 | 2009-04-04 07:22:01 +0000 | [diff] [blame] | 1777 | ID.AddPointer(*I); |
| 1778 | } |
| 1779 | |
Owen Anderson | 0d2de8c | 2009-06-20 00:24:58 +0000 | [diff] [blame] | 1780 | MDNode *MDNode::get(Value*const* Vals, unsigned NumVals) { |
Nick Lewycky | 49f8919 | 2009-04-04 07:22:01 +0000 | [diff] [blame] | 1781 | FoldingSetNodeID ID; |
| 1782 | for (unsigned i = 0; i != NumVals; ++i) |
| 1783 | ID.AddPointer(Vals[i]); |
| 1784 | |
Owen Anderson | 0d2de8c | 2009-06-20 00:24:58 +0000 | [diff] [blame] | 1785 | ConstantsLock->reader_acquire(); |
Owen Anderson | d830eb8 | 2009-06-18 19:10:19 +0000 | [diff] [blame] | 1786 | void *InsertPoint; |
| 1787 | MDNode *N = MDNodeSet->FindNodeOrInsertPos(ID, InsertPoint); |
Owen Anderson | 0d2de8c | 2009-06-20 00:24:58 +0000 | [diff] [blame] | 1788 | ConstantsLock->reader_release(); |
Owen Anderson | d830eb8 | 2009-06-18 19:10:19 +0000 | [diff] [blame] | 1789 | |
| 1790 | if (!N) { |
Owen Anderson | 5c96ef7 | 2009-07-07 18:33:04 +0000 | [diff] [blame] | 1791 | sys::SmartScopedWriter<true> Writer(*ConstantsLock); |
Owen Anderson | 0d2de8c | 2009-06-20 00:24:58 +0000 | [diff] [blame] | 1792 | N = MDNodeSet->FindNodeOrInsertPos(ID, InsertPoint); |
| 1793 | if (!N) { |
Owen Anderson | d830eb8 | 2009-06-18 19:10:19 +0000 | [diff] [blame] | 1794 | // InsertPoint will have been set by the FindNodeOrInsertPos call. |
| 1795 | N = new(0) MDNode(Vals, NumVals); |
| 1796 | MDNodeSet->InsertNode(N, InsertPoint); |
Owen Anderson | 2d7231d | 2009-06-17 18:40:29 +0000 | [diff] [blame] | 1797 | } |
Owen Anderson | 2d7231d | 2009-06-17 18:40:29 +0000 | [diff] [blame] | 1798 | } |
Owen Anderson | d830eb8 | 2009-06-18 19:10:19 +0000 | [diff] [blame] | 1799 | return N; |
Nick Lewycky | 49f8919 | 2009-04-04 07:22:01 +0000 | [diff] [blame] | 1800 | } |
| 1801 | |
Owen Anderson | 0d2de8c | 2009-06-20 00:24:58 +0000 | [diff] [blame] | 1802 | void MDNode::destroyConstant() { |
Owen Anderson | 5c96ef7 | 2009-07-07 18:33:04 +0000 | [diff] [blame] | 1803 | sys::SmartScopedWriter<true> Writer(*ConstantsLock); |
Owen Anderson | d830eb8 | 2009-06-18 19:10:19 +0000 | [diff] [blame] | 1804 | MDNodeSet->RemoveNode(this); |
Owen Anderson | 0d2de8c | 2009-06-20 00:24:58 +0000 | [diff] [blame] | 1805 | |
Nick Lewycky | 49f8919 | 2009-04-04 07:22:01 +0000 | [diff] [blame] | 1806 | destroyConstantImpl(); |
| 1807 | } |
Chris Lattner | d5f67d8 | 2004-10-16 18:07:16 +0000 | [diff] [blame] | 1808 | |
Vikram S. Adve | 4e537b2 | 2002-07-14 23:13:17 +0000 | [diff] [blame] | 1809 | //---- ConstantExpr::get() implementations... |
Vikram S. Adve | 4e537b2 | 2002-07-14 23:13:17 +0000 | [diff] [blame] | 1810 | // |
Reid Spencer | 8d9336d | 2006-12-31 05:26:44 +0000 | [diff] [blame] | 1811 | |
Dan Gohman | d78c400 | 2008-05-13 00:00:25 +0000 | [diff] [blame] | 1812 | namespace { |
| 1813 | |
Reid Spencer | ee3c991 | 2006-12-04 05:19:50 +0000 | [diff] [blame] | 1814 | struct ExprMapKeyType { |
Dan Gohman | 1ecaf45 | 2008-05-31 00:58:22 +0000 | [diff] [blame] | 1815 | typedef SmallVector<unsigned, 4> IndexList; |
| 1816 | |
| 1817 | ExprMapKeyType(unsigned opc, |
| 1818 | const std::vector<Constant*> &ops, |
| 1819 | unsigned short pred = 0, |
| 1820 | const IndexList &inds = IndexList()) |
| 1821 | : opcode(opc), predicate(pred), operands(ops), indices(inds) {} |
Reid Spencer | dba6aa4 | 2006-12-04 18:38:05 +0000 | [diff] [blame] | 1822 | uint16_t opcode; |
| 1823 | uint16_t predicate; |
Reid Spencer | ee3c991 | 2006-12-04 05:19:50 +0000 | [diff] [blame] | 1824 | std::vector<Constant*> operands; |
Dan Gohman | 1ecaf45 | 2008-05-31 00:58:22 +0000 | [diff] [blame] | 1825 | IndexList indices; |
Reid Spencer | ee3c991 | 2006-12-04 05:19:50 +0000 | [diff] [blame] | 1826 | bool operator==(const ExprMapKeyType& that) const { |
| 1827 | return this->opcode == that.opcode && |
| 1828 | this->predicate == that.predicate && |
Bill Wendling | 97f7de8 | 2008-10-26 00:19:56 +0000 | [diff] [blame] | 1829 | this->operands == that.operands && |
Dan Gohman | 1ecaf45 | 2008-05-31 00:58:22 +0000 | [diff] [blame] | 1830 | this->indices == that.indices; |
Reid Spencer | ee3c991 | 2006-12-04 05:19:50 +0000 | [diff] [blame] | 1831 | } |
| 1832 | bool operator<(const ExprMapKeyType & that) const { |
| 1833 | return this->opcode < that.opcode || |
| 1834 | (this->opcode == that.opcode && this->predicate < that.predicate) || |
| 1835 | (this->opcode == that.opcode && this->predicate == that.predicate && |
Dan Gohman | 1ecaf45 | 2008-05-31 00:58:22 +0000 | [diff] [blame] | 1836 | this->operands < that.operands) || |
| 1837 | (this->opcode == that.opcode && this->predicate == that.predicate && |
| 1838 | this->operands == that.operands && this->indices < that.indices); |
Reid Spencer | ee3c991 | 2006-12-04 05:19:50 +0000 | [diff] [blame] | 1839 | } |
| 1840 | |
| 1841 | bool operator!=(const ExprMapKeyType& that) const { |
| 1842 | return !(*this == that); |
| 1843 | } |
| 1844 | }; |
Chris Lattner | 98fa07b | 2003-05-23 20:03:32 +0000 | [diff] [blame] | 1845 | |
Dan Gohman | d78c400 | 2008-05-13 00:00:25 +0000 | [diff] [blame] | 1846 | } |
| 1847 | |
Chris Lattner | 189d19f | 2003-11-21 20:23:48 +0000 | [diff] [blame] | 1848 | namespace llvm { |
| 1849 | template<> |
| 1850 | struct ConstantCreator<ConstantExpr, Type, ExprMapKeyType> { |
Reid Spencer | 10fbf0e | 2006-12-03 05:48:19 +0000 | [diff] [blame] | 1851 | static ConstantExpr *create(const Type *Ty, const ExprMapKeyType &V, |
| 1852 | unsigned short pred = 0) { |
Reid Spencer | ee3c991 | 2006-12-04 05:19:50 +0000 | [diff] [blame] | 1853 | if (Instruction::isCast(V.opcode)) |
| 1854 | return new UnaryConstantExpr(V.opcode, V.operands[0], Ty); |
| 1855 | if ((V.opcode >= Instruction::BinaryOpsBegin && |
Reid Spencer | 2341c22 | 2007-02-02 02:16:23 +0000 | [diff] [blame] | 1856 | V.opcode < Instruction::BinaryOpsEnd)) |
Reid Spencer | ee3c991 | 2006-12-04 05:19:50 +0000 | [diff] [blame] | 1857 | return new BinaryConstantExpr(V.opcode, V.operands[0], V.operands[1]); |
| 1858 | if (V.opcode == Instruction::Select) |
| 1859 | return new SelectConstantExpr(V.operands[0], V.operands[1], |
| 1860 | V.operands[2]); |
| 1861 | if (V.opcode == Instruction::ExtractElement) |
| 1862 | return new ExtractElementConstantExpr(V.operands[0], V.operands[1]); |
| 1863 | if (V.opcode == Instruction::InsertElement) |
| 1864 | return new InsertElementConstantExpr(V.operands[0], V.operands[1], |
| 1865 | V.operands[2]); |
| 1866 | if (V.opcode == Instruction::ShuffleVector) |
| 1867 | return new ShuffleVectorConstantExpr(V.operands[0], V.operands[1], |
| 1868 | V.operands[2]); |
Dan Gohman | 1ecaf45 | 2008-05-31 00:58:22 +0000 | [diff] [blame] | 1869 | if (V.opcode == Instruction::InsertValue) |
| 1870 | return new InsertValueConstantExpr(V.operands[0], V.operands[1], |
| 1871 | V.indices, Ty); |
| 1872 | if (V.opcode == Instruction::ExtractValue) |
| 1873 | return new ExtractValueConstantExpr(V.operands[0], V.indices, Ty); |
Reid Spencer | ee3c991 | 2006-12-04 05:19:50 +0000 | [diff] [blame] | 1874 | if (V.opcode == Instruction::GetElementPtr) { |
| 1875 | std::vector<Constant*> IdxList(V.operands.begin()+1, V.operands.end()); |
Gabor Greif | e9ecc68 | 2008-04-06 20:25:17 +0000 | [diff] [blame] | 1876 | return GetElementPtrConstantExpr::Create(V.operands[0], IdxList, Ty); |
Reid Spencer | ee3c991 | 2006-12-04 05:19:50 +0000 | [diff] [blame] | 1877 | } |
Misha Brukman | b1c9317 | 2005-04-21 23:48:37 +0000 | [diff] [blame] | 1878 | |
Reid Spencer | ee3c991 | 2006-12-04 05:19:50 +0000 | [diff] [blame] | 1879 | // The compare instructions are weird. We have to encode the predicate |
| 1880 | // value and it is combined with the instruction opcode by multiplying |
| 1881 | // the opcode by one hundred. We must decode this to get the predicate. |
| 1882 | if (V.opcode == Instruction::ICmp) |
Nate Begeman | d219570 | 2008-05-12 19:01:56 +0000 | [diff] [blame] | 1883 | return new CompareConstantExpr(Ty, Instruction::ICmp, V.predicate, |
Reid Spencer | ee3c991 | 2006-12-04 05:19:50 +0000 | [diff] [blame] | 1884 | V.operands[0], V.operands[1]); |
| 1885 | if (V.opcode == Instruction::FCmp) |
Nate Begeman | d219570 | 2008-05-12 19:01:56 +0000 | [diff] [blame] | 1886 | return new CompareConstantExpr(Ty, Instruction::FCmp, V.predicate, |
| 1887 | V.operands[0], V.operands[1]); |
Torok Edwin | 56d0659 | 2009-07-11 20:10:48 +0000 | [diff] [blame] | 1888 | LLVM_UNREACHABLE("Invalid ConstantExpr!"); |
Jeff Cohen | 9f46963 | 2006-12-15 21:47:01 +0000 | [diff] [blame] | 1889 | return 0; |
Chris Lattner | b50d135 | 2003-10-05 00:17:43 +0000 | [diff] [blame] | 1890 | } |
Chris Lattner | 189d19f | 2003-11-21 20:23:48 +0000 | [diff] [blame] | 1891 | }; |
Chris Lattner | b50d135 | 2003-10-05 00:17:43 +0000 | [diff] [blame] | 1892 | |
Chris Lattner | 189d19f | 2003-11-21 20:23:48 +0000 | [diff] [blame] | 1893 | template<> |
| 1894 | struct ConvertConstantType<ConstantExpr, Type> { |
| 1895 | static void convert(ConstantExpr *OldC, const Type *NewTy) { |
| 1896 | Constant *New; |
| 1897 | switch (OldC->getOpcode()) { |
Reid Spencer | 6c38f0b | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 1898 | case Instruction::Trunc: |
| 1899 | case Instruction::ZExt: |
| 1900 | case Instruction::SExt: |
| 1901 | case Instruction::FPTrunc: |
| 1902 | case Instruction::FPExt: |
| 1903 | case Instruction::UIToFP: |
| 1904 | case Instruction::SIToFP: |
| 1905 | case Instruction::FPToUI: |
| 1906 | case Instruction::FPToSI: |
| 1907 | case Instruction::PtrToInt: |
| 1908 | case Instruction::IntToPtr: |
| 1909 | case Instruction::BitCast: |
Reid Spencer | bb65ebf | 2006-12-12 23:36:14 +0000 | [diff] [blame] | 1910 | New = ConstantExpr::getCast(OldC->getOpcode(), OldC->getOperand(0), |
Owen Anderson | 0d2de8c | 2009-06-20 00:24:58 +0000 | [diff] [blame] | 1911 | NewTy); |
Chris Lattner | 189d19f | 2003-11-21 20:23:48 +0000 | [diff] [blame] | 1912 | break; |
Chris Lattner | 6e415c0 | 2004-03-12 05:54:04 +0000 | [diff] [blame] | 1913 | case Instruction::Select: |
| 1914 | New = ConstantExpr::getSelectTy(NewTy, OldC->getOperand(0), |
| 1915 | OldC->getOperand(1), |
Owen Anderson | 0d2de8c | 2009-06-20 00:24:58 +0000 | [diff] [blame] | 1916 | OldC->getOperand(2)); |
Chris Lattner | 6e415c0 | 2004-03-12 05:54:04 +0000 | [diff] [blame] | 1917 | break; |
Chris Lattner | 189d19f | 2003-11-21 20:23:48 +0000 | [diff] [blame] | 1918 | default: |
| 1919 | assert(OldC->getOpcode() >= Instruction::BinaryOpsBegin && |
Reid Spencer | 7eb55b3 | 2006-11-02 01:53:59 +0000 | [diff] [blame] | 1920 | OldC->getOpcode() < Instruction::BinaryOpsEnd); |
Chris Lattner | 189d19f | 2003-11-21 20:23:48 +0000 | [diff] [blame] | 1921 | New = ConstantExpr::getTy(NewTy, OldC->getOpcode(), OldC->getOperand(0), |
Owen Anderson | 0d2de8c | 2009-06-20 00:24:58 +0000 | [diff] [blame] | 1922 | OldC->getOperand(1)); |
Chris Lattner | 189d19f | 2003-11-21 20:23:48 +0000 | [diff] [blame] | 1923 | break; |
| 1924 | case Instruction::GetElementPtr: |
Misha Brukman | b1c9317 | 2005-04-21 23:48:37 +0000 | [diff] [blame] | 1925 | // Make everyone now use a constant of the new type... |
Chris Lattner | 13128ab | 2004-10-11 22:52:25 +0000 | [diff] [blame] | 1926 | std::vector<Value*> Idx(OldC->op_begin()+1, OldC->op_end()); |
Chris Lattner | 302116a | 2007-01-31 04:40:28 +0000 | [diff] [blame] | 1927 | New = ConstantExpr::getGetElementPtrTy(NewTy, OldC->getOperand(0), |
Owen Anderson | 0d2de8c | 2009-06-20 00:24:58 +0000 | [diff] [blame] | 1928 | &Idx[0], Idx.size()); |
Chris Lattner | 189d19f | 2003-11-21 20:23:48 +0000 | [diff] [blame] | 1929 | break; |
| 1930 | } |
Misha Brukman | b1c9317 | 2005-04-21 23:48:37 +0000 | [diff] [blame] | 1931 | |
Chris Lattner | 189d19f | 2003-11-21 20:23:48 +0000 | [diff] [blame] | 1932 | assert(New != OldC && "Didn't replace constant??"); |
| 1933 | OldC->uncheckedReplaceAllUsesWith(New); |
Owen Anderson | 0d2de8c | 2009-06-20 00:24:58 +0000 | [diff] [blame] | 1934 | OldC->destroyConstant(); // This constant is now dead, destroy it. |
Chris Lattner | 189d19f | 2003-11-21 20:23:48 +0000 | [diff] [blame] | 1935 | } |
| 1936 | }; |
| 1937 | } // end namespace llvm |
Chris Lattner | b50d135 | 2003-10-05 00:17:43 +0000 | [diff] [blame] | 1938 | |
| 1939 | |
Chris Lattner | 3e650af | 2004-08-04 04:48:01 +0000 | [diff] [blame] | 1940 | static ExprMapKeyType getValType(ConstantExpr *CE) { |
| 1941 | std::vector<Constant*> Operands; |
| 1942 | Operands.reserve(CE->getNumOperands()); |
| 1943 | for (unsigned i = 0, e = CE->getNumOperands(); i != e; ++i) |
| 1944 | Operands.push_back(cast<Constant>(CE->getOperand(i))); |
Reid Spencer | ee3c991 | 2006-12-04 05:19:50 +0000 | [diff] [blame] | 1945 | return ExprMapKeyType(CE->getOpcode(), Operands, |
Dan Gohman | 1ecaf45 | 2008-05-31 00:58:22 +0000 | [diff] [blame] | 1946 | CE->isCompare() ? CE->getPredicate() : 0, |
| 1947 | CE->hasIndices() ? |
| 1948 | CE->getIndices() : SmallVector<unsigned, 4>()); |
Chris Lattner | 3e650af | 2004-08-04 04:48:01 +0000 | [diff] [blame] | 1949 | } |
| 1950 | |
Chris Lattner | 69edc98 | 2006-09-28 00:35:06 +0000 | [diff] [blame] | 1951 | static ManagedStatic<ValueMap<ExprMapKeyType, Type, |
| 1952 | ConstantExpr> > ExprConstants; |
Vikram S. Adve | 4c48533 | 2002-07-15 18:19:33 +0000 | [diff] [blame] | 1953 | |
Reid Spencer | 6c38f0b | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 1954 | /// 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] | 1955 | /// 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] | 1956 | static inline Constant *getFoldedCast( |
Owen Anderson | 0d2de8c | 2009-06-20 00:24:58 +0000 | [diff] [blame] | 1957 | Instruction::CastOps opc, Constant *C, const Type *Ty) { |
Chris Lattner | 815ae2b | 2003-10-07 22:19:19 +0000 | [diff] [blame] | 1958 | assert(Ty->isFirstClassType() && "Cannot cast to an aggregate type!"); |
Reid Spencer | 6c38f0b | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 1959 | // Fold a few common cases |
Owen Anderson | 53a5221 | 2009-07-13 04:09:18 +0000 | [diff] [blame^] | 1960 | if (Constant *FC = |
| 1961 | ConstantFoldCastInstruction(getGlobalContext(), opc, C, Ty)) |
Reid Spencer | 6c38f0b | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 1962 | return FC; |
Chris Lattner | acdbe71 | 2003-04-17 19:24:48 +0000 | [diff] [blame] | 1963 | |
Vikram S. Adve | 4c48533 | 2002-07-15 18:19:33 +0000 | [diff] [blame] | 1964 | // Look up the constant in the table first to ensure uniqueness |
Chris Lattner | 2b383d2e | 2003-05-13 21:37:02 +0000 | [diff] [blame] | 1965 | std::vector<Constant*> argVec(1, C); |
Reid Spencer | ee3c991 | 2006-12-04 05:19:50 +0000 | [diff] [blame] | 1966 | ExprMapKeyType Key(opc, argVec); |
Owen Anderson | 2d7231d | 2009-06-17 18:40:29 +0000 | [diff] [blame] | 1967 | |
Owen Anderson | 6179404 | 2009-06-17 20:10:08 +0000 | [diff] [blame] | 1968 | // Implicitly locked. |
Owen Anderson | 0d2de8c | 2009-06-20 00:24:58 +0000 | [diff] [blame] | 1969 | return ExprConstants->getOrCreate(Ty, Key); |
Vikram S. Adve | 4e537b2 | 2002-07-14 23:13:17 +0000 | [diff] [blame] | 1970 | } |
Reid Spencer | f37dc65 | 2006-12-05 19:14:13 +0000 | [diff] [blame] | 1971 | |
Owen Anderson | 0d2de8c | 2009-06-20 00:24:58 +0000 | [diff] [blame] | 1972 | Constant *ConstantExpr::getCast(unsigned oc, Constant *C, const Type *Ty) { |
Reid Spencer | 6c38f0b | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 1973 | Instruction::CastOps opc = Instruction::CastOps(oc); |
| 1974 | assert(Instruction::isCast(opc) && "opcode out of range"); |
| 1975 | assert(C && Ty && "Null arguments to getCast"); |
| 1976 | assert(Ty->isFirstClassType() && "Cannot cast to an aggregate type!"); |
| 1977 | |
| 1978 | switch (opc) { |
| 1979 | default: |
Torok Edwin | 56d0659 | 2009-07-11 20:10:48 +0000 | [diff] [blame] | 1980 | LLVM_UNREACHABLE("Invalid cast opcode"); |
Reid Spencer | 6c38f0b | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 1981 | break; |
Owen Anderson | 0d2de8c | 2009-06-20 00:24:58 +0000 | [diff] [blame] | 1982 | case Instruction::Trunc: return getTrunc(C, Ty); |
| 1983 | case Instruction::ZExt: return getZExt(C, Ty); |
| 1984 | case Instruction::SExt: return getSExt(C, Ty); |
| 1985 | case Instruction::FPTrunc: return getFPTrunc(C, Ty); |
| 1986 | case Instruction::FPExt: return getFPExtend(C, Ty); |
| 1987 | case Instruction::UIToFP: return getUIToFP(C, Ty); |
| 1988 | case Instruction::SIToFP: return getSIToFP(C, Ty); |
| 1989 | case Instruction::FPToUI: return getFPToUI(C, Ty); |
| 1990 | case Instruction::FPToSI: return getFPToSI(C, Ty); |
| 1991 | case Instruction::PtrToInt: return getPtrToInt(C, Ty); |
| 1992 | case Instruction::IntToPtr: return getIntToPtr(C, Ty); |
| 1993 | case Instruction::BitCast: return getBitCast(C, Ty); |
Chris Lattner | 1ece6f8 | 2005-01-01 15:59:57 +0000 | [diff] [blame] | 1994 | } |
Reid Spencer | 6c38f0b | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 1995 | return 0; |
Reid Spencer | f37dc65 | 2006-12-05 19:14:13 +0000 | [diff] [blame] | 1996 | } |
| 1997 | |
Reid Spencer | 5c14088 | 2006-12-04 20:17:56 +0000 | [diff] [blame] | 1998 | Constant *ConstantExpr::getZExtOrBitCast(Constant *C, const Type *Ty) { |
Dan Gohman | 7ccc52f | 2009-06-15 22:12:54 +0000 | [diff] [blame] | 1999 | if (C->getType()->getScalarSizeInBits() == Ty->getScalarSizeInBits()) |
Reid Spencer | 5c14088 | 2006-12-04 20:17:56 +0000 | [diff] [blame] | 2000 | return getCast(Instruction::BitCast, C, Ty); |
| 2001 | return getCast(Instruction::ZExt, C, Ty); |
| 2002 | } |
| 2003 | |
Owen Anderson | 0d2de8c | 2009-06-20 00:24:58 +0000 | [diff] [blame] | 2004 | Constant *ConstantExpr::getSExtOrBitCast(Constant *C, const Type *Ty) { |
Dan Gohman | 7ccc52f | 2009-06-15 22:12:54 +0000 | [diff] [blame] | 2005 | if (C->getType()->getScalarSizeInBits() == Ty->getScalarSizeInBits()) |
Owen Anderson | 0d2de8c | 2009-06-20 00:24:58 +0000 | [diff] [blame] | 2006 | return getCast(Instruction::BitCast, C, Ty); |
| 2007 | return getCast(Instruction::SExt, C, Ty); |
Reid Spencer | 5c14088 | 2006-12-04 20:17:56 +0000 | [diff] [blame] | 2008 | } |
| 2009 | |
| 2010 | Constant *ConstantExpr::getTruncOrBitCast(Constant *C, const Type *Ty) { |
Dan Gohman | 7ccc52f | 2009-06-15 22:12:54 +0000 | [diff] [blame] | 2011 | if (C->getType()->getScalarSizeInBits() == Ty->getScalarSizeInBits()) |
Reid Spencer | 5c14088 | 2006-12-04 20:17:56 +0000 | [diff] [blame] | 2012 | return getCast(Instruction::BitCast, C, Ty); |
| 2013 | return getCast(Instruction::Trunc, C, Ty); |
| 2014 | } |
| 2015 | |
Owen Anderson | 0d2de8c | 2009-06-20 00:24:58 +0000 | [diff] [blame] | 2016 | Constant *ConstantExpr::getPointerCast(Constant *S, const Type *Ty) { |
Reid Spencer | bc245a0 | 2006-12-05 03:25:26 +0000 | [diff] [blame] | 2017 | assert(isa<PointerType>(S->getType()) && "Invalid cast"); |
Chris Lattner | 03c4953 | 2007-01-15 02:27:26 +0000 | [diff] [blame] | 2018 | assert((Ty->isInteger() || isa<PointerType>(Ty)) && "Invalid cast"); |
Reid Spencer | bc245a0 | 2006-12-05 03:25:26 +0000 | [diff] [blame] | 2019 | |
Chris Lattner | 03c4953 | 2007-01-15 02:27:26 +0000 | [diff] [blame] | 2020 | if (Ty->isInteger()) |
Owen Anderson | 0d2de8c | 2009-06-20 00:24:58 +0000 | [diff] [blame] | 2021 | return getCast(Instruction::PtrToInt, S, Ty); |
| 2022 | return getCast(Instruction::BitCast, S, Ty); |
Reid Spencer | bc245a0 | 2006-12-05 03:25:26 +0000 | [diff] [blame] | 2023 | } |
| 2024 | |
Reid Spencer | 56521c4 | 2006-12-12 00:51:07 +0000 | [diff] [blame] | 2025 | Constant *ConstantExpr::getIntegerCast(Constant *C, const Type *Ty, |
| 2026 | bool isSigned) { |
Dan Gohman | 7ccc52f | 2009-06-15 22:12:54 +0000 | [diff] [blame] | 2027 | assert(C->getType()->isIntOrIntVector() && |
| 2028 | Ty->isIntOrIntVector() && "Invalid cast"); |
| 2029 | unsigned SrcBits = C->getType()->getScalarSizeInBits(); |
| 2030 | unsigned DstBits = Ty->getScalarSizeInBits(); |
Reid Spencer | 56521c4 | 2006-12-12 00:51:07 +0000 | [diff] [blame] | 2031 | Instruction::CastOps opcode = |
| 2032 | (SrcBits == DstBits ? Instruction::BitCast : |
| 2033 | (SrcBits > DstBits ? Instruction::Trunc : |
| 2034 | (isSigned ? Instruction::SExt : Instruction::ZExt))); |
| 2035 | return getCast(opcode, C, Ty); |
| 2036 | } |
| 2037 | |
| 2038 | Constant *ConstantExpr::getFPCast(Constant *C, const Type *Ty) { |
Dan Gohman | 7ccc52f | 2009-06-15 22:12:54 +0000 | [diff] [blame] | 2039 | assert(C->getType()->isFPOrFPVector() && Ty->isFPOrFPVector() && |
Reid Spencer | 56521c4 | 2006-12-12 00:51:07 +0000 | [diff] [blame] | 2040 | "Invalid cast"); |
Dan Gohman | 7ccc52f | 2009-06-15 22:12:54 +0000 | [diff] [blame] | 2041 | unsigned SrcBits = C->getType()->getScalarSizeInBits(); |
| 2042 | unsigned DstBits = Ty->getScalarSizeInBits(); |
Reid Spencer | ca104e8 | 2006-12-12 05:38:50 +0000 | [diff] [blame] | 2043 | if (SrcBits == DstBits) |
| 2044 | return C; // Avoid a useless cast |
Reid Spencer | 56521c4 | 2006-12-12 00:51:07 +0000 | [diff] [blame] | 2045 | Instruction::CastOps opcode = |
Reid Spencer | ca104e8 | 2006-12-12 05:38:50 +0000 | [diff] [blame] | 2046 | (SrcBits > DstBits ? Instruction::FPTrunc : Instruction::FPExt); |
Reid Spencer | 56521c4 | 2006-12-12 00:51:07 +0000 | [diff] [blame] | 2047 | return getCast(opcode, C, Ty); |
| 2048 | } |
| 2049 | |
Owen Anderson | 0d2de8c | 2009-06-20 00:24:58 +0000 | [diff] [blame] | 2050 | Constant *ConstantExpr::getTrunc(Constant *C, const Type *Ty) { |
Dan Gohman | 7ccc52f | 2009-06-15 22:12:54 +0000 | [diff] [blame] | 2051 | #ifndef NDEBUG |
| 2052 | bool fromVec = C->getType()->getTypeID() == Type::VectorTyID; |
| 2053 | bool toVec = Ty->getTypeID() == Type::VectorTyID; |
| 2054 | #endif |
| 2055 | assert((fromVec == toVec) && "Cannot convert from scalar to/from vector"); |
| 2056 | assert(C->getType()->isIntOrIntVector() && "Trunc operand must be integer"); |
| 2057 | assert(Ty->isIntOrIntVector() && "Trunc produces only integral"); |
| 2058 | assert(C->getType()->getScalarSizeInBits() > Ty->getScalarSizeInBits()&& |
Reid Spencer | 6c38f0b | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 2059 | "SrcTy must be larger than DestTy for Trunc!"); |
| 2060 | |
Owen Anderson | 0d2de8c | 2009-06-20 00:24:58 +0000 | [diff] [blame] | 2061 | return getFoldedCast(Instruction::Trunc, C, Ty); |
Reid Spencer | 6c38f0b | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 2062 | } |
| 2063 | |
Owen Anderson | 0d2de8c | 2009-06-20 00:24:58 +0000 | [diff] [blame] | 2064 | Constant *ConstantExpr::getSExt(Constant *C, const Type *Ty) { |
Dan Gohman | 7ccc52f | 2009-06-15 22:12:54 +0000 | [diff] [blame] | 2065 | #ifndef NDEBUG |
| 2066 | bool fromVec = C->getType()->getTypeID() == Type::VectorTyID; |
| 2067 | bool toVec = Ty->getTypeID() == Type::VectorTyID; |
| 2068 | #endif |
| 2069 | assert((fromVec == toVec) && "Cannot convert from scalar to/from vector"); |
| 2070 | assert(C->getType()->isIntOrIntVector() && "SExt operand must be integral"); |
| 2071 | assert(Ty->isIntOrIntVector() && "SExt produces only integer"); |
| 2072 | assert(C->getType()->getScalarSizeInBits() < Ty->getScalarSizeInBits()&& |
Reid Spencer | 6c38f0b | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 2073 | "SrcTy must be smaller than DestTy for SExt!"); |
| 2074 | |
Owen Anderson | 0d2de8c | 2009-06-20 00:24:58 +0000 | [diff] [blame] | 2075 | return getFoldedCast(Instruction::SExt, C, Ty); |
Chris Lattner | dd28474 | 2004-04-04 23:20:30 +0000 | [diff] [blame] | 2076 | } |
| 2077 | |
Owen Anderson | 0d2de8c | 2009-06-20 00:24:58 +0000 | [diff] [blame] | 2078 | Constant *ConstantExpr::getZExt(Constant *C, const Type *Ty) { |
Dan Gohman | 7ccc52f | 2009-06-15 22:12:54 +0000 | [diff] [blame] | 2079 | #ifndef NDEBUG |
| 2080 | bool fromVec = C->getType()->getTypeID() == Type::VectorTyID; |
| 2081 | bool toVec = Ty->getTypeID() == Type::VectorTyID; |
| 2082 | #endif |
| 2083 | assert((fromVec == toVec) && "Cannot convert from scalar to/from vector"); |
| 2084 | assert(C->getType()->isIntOrIntVector() && "ZEXt operand must be integral"); |
| 2085 | assert(Ty->isIntOrIntVector() && "ZExt produces only integer"); |
| 2086 | assert(C->getType()->getScalarSizeInBits() < Ty->getScalarSizeInBits()&& |
Reid Spencer | 6c38f0b | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 2087 | "SrcTy must be smaller than DestTy for ZExt!"); |
| 2088 | |
Owen Anderson | 0d2de8c | 2009-06-20 00:24:58 +0000 | [diff] [blame] | 2089 | return getFoldedCast(Instruction::ZExt, C, Ty); |
Reid Spencer | 6c38f0b | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 2090 | } |
| 2091 | |
Owen Anderson | 0d2de8c | 2009-06-20 00:24:58 +0000 | [diff] [blame] | 2092 | Constant *ConstantExpr::getFPTrunc(Constant *C, const Type *Ty) { |
Dan Gohman | 7ccc52f | 2009-06-15 22:12:54 +0000 | [diff] [blame] | 2093 | #ifndef NDEBUG |
| 2094 | bool fromVec = C->getType()->getTypeID() == Type::VectorTyID; |
| 2095 | bool toVec = Ty->getTypeID() == Type::VectorTyID; |
| 2096 | #endif |
| 2097 | assert((fromVec == toVec) && "Cannot convert from scalar to/from vector"); |
| 2098 | assert(C->getType()->isFPOrFPVector() && Ty->isFPOrFPVector() && |
| 2099 | C->getType()->getScalarSizeInBits() > Ty->getScalarSizeInBits()&& |
Reid Spencer | 6c38f0b | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 2100 | "This is an illegal floating point truncation!"); |
Owen Anderson | 0d2de8c | 2009-06-20 00:24:58 +0000 | [diff] [blame] | 2101 | return getFoldedCast(Instruction::FPTrunc, C, Ty); |
Reid Spencer | 6c38f0b | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 2102 | } |
| 2103 | |
Owen Anderson | 0d2de8c | 2009-06-20 00:24:58 +0000 | [diff] [blame] | 2104 | Constant *ConstantExpr::getFPExtend(Constant *C, const Type *Ty) { |
Dan Gohman | 7ccc52f | 2009-06-15 22:12:54 +0000 | [diff] [blame] | 2105 | #ifndef NDEBUG |
| 2106 | bool fromVec = C->getType()->getTypeID() == Type::VectorTyID; |
| 2107 | bool toVec = Ty->getTypeID() == Type::VectorTyID; |
| 2108 | #endif |
| 2109 | assert((fromVec == toVec) && "Cannot convert from scalar to/from vector"); |
| 2110 | assert(C->getType()->isFPOrFPVector() && Ty->isFPOrFPVector() && |
| 2111 | C->getType()->getScalarSizeInBits() < Ty->getScalarSizeInBits()&& |
Reid Spencer | 6c38f0b | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 2112 | "This is an illegal floating point extension!"); |
Owen Anderson | 0d2de8c | 2009-06-20 00:24:58 +0000 | [diff] [blame] | 2113 | return getFoldedCast(Instruction::FPExt, C, Ty); |
Reid Spencer | 6c38f0b | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 2114 | } |
| 2115 | |
Owen Anderson | 0d2de8c | 2009-06-20 00:24:58 +0000 | [diff] [blame] | 2116 | Constant *ConstantExpr::getUIToFP(Constant *C, const Type *Ty) { |
Devang Patel | d26344d | 2008-11-03 23:20:04 +0000 | [diff] [blame] | 2117 | #ifndef NDEBUG |
Nate Begeman | d4d45c2 | 2007-11-17 03:58:34 +0000 | [diff] [blame] | 2118 | bool fromVec = C->getType()->getTypeID() == Type::VectorTyID; |
| 2119 | bool toVec = Ty->getTypeID() == Type::VectorTyID; |
Devang Patel | d26344d | 2008-11-03 23:20:04 +0000 | [diff] [blame] | 2120 | #endif |
Nate Begeman | d4d45c2 | 2007-11-17 03:58:34 +0000 | [diff] [blame] | 2121 | assert((fromVec == toVec) && "Cannot convert from scalar to/from vector"); |
| 2122 | assert(C->getType()->isIntOrIntVector() && Ty->isFPOrFPVector() && |
| 2123 | "This is an illegal uint to floating point cast!"); |
Owen Anderson | 0d2de8c | 2009-06-20 00:24:58 +0000 | [diff] [blame] | 2124 | return getFoldedCast(Instruction::UIToFP, C, Ty); |
Reid Spencer | 6c38f0b | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 2125 | } |
| 2126 | |
Owen Anderson | 0d2de8c | 2009-06-20 00:24:58 +0000 | [diff] [blame] | 2127 | Constant *ConstantExpr::getSIToFP(Constant *C, const Type *Ty) { |
Devang Patel | d26344d | 2008-11-03 23:20:04 +0000 | [diff] [blame] | 2128 | #ifndef NDEBUG |
Nate Begeman | d4d45c2 | 2007-11-17 03:58:34 +0000 | [diff] [blame] | 2129 | bool fromVec = C->getType()->getTypeID() == Type::VectorTyID; |
| 2130 | bool toVec = Ty->getTypeID() == Type::VectorTyID; |
Devang Patel | d26344d | 2008-11-03 23:20:04 +0000 | [diff] [blame] | 2131 | #endif |
Nate Begeman | d4d45c2 | 2007-11-17 03:58:34 +0000 | [diff] [blame] | 2132 | assert((fromVec == toVec) && "Cannot convert from scalar to/from vector"); |
| 2133 | assert(C->getType()->isIntOrIntVector() && Ty->isFPOrFPVector() && |
Reid Spencer | 6c38f0b | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 2134 | "This is an illegal sint to floating point cast!"); |
Owen Anderson | 0d2de8c | 2009-06-20 00:24:58 +0000 | [diff] [blame] | 2135 | return getFoldedCast(Instruction::SIToFP, C, Ty); |
Reid Spencer | 6c38f0b | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 2136 | } |
| 2137 | |
Owen Anderson | 0d2de8c | 2009-06-20 00:24:58 +0000 | [diff] [blame] | 2138 | Constant *ConstantExpr::getFPToUI(Constant *C, const Type *Ty) { |
Devang Patel | d26344d | 2008-11-03 23:20:04 +0000 | [diff] [blame] | 2139 | #ifndef NDEBUG |
Nate Begeman | d4d45c2 | 2007-11-17 03:58:34 +0000 | [diff] [blame] | 2140 | bool fromVec = C->getType()->getTypeID() == Type::VectorTyID; |
| 2141 | bool toVec = Ty->getTypeID() == Type::VectorTyID; |
Devang Patel | d26344d | 2008-11-03 23:20:04 +0000 | [diff] [blame] | 2142 | #endif |
Nate Begeman | d4d45c2 | 2007-11-17 03:58:34 +0000 | [diff] [blame] | 2143 | assert((fromVec == toVec) && "Cannot convert from scalar to/from vector"); |
| 2144 | assert(C->getType()->isFPOrFPVector() && Ty->isIntOrIntVector() && |
| 2145 | "This is an illegal floating point to uint cast!"); |
Owen Anderson | 0d2de8c | 2009-06-20 00:24:58 +0000 | [diff] [blame] | 2146 | return getFoldedCast(Instruction::FPToUI, C, Ty); |
Reid Spencer | 6c38f0b | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 2147 | } |
| 2148 | |
Owen Anderson | 0d2de8c | 2009-06-20 00:24:58 +0000 | [diff] [blame] | 2149 | Constant *ConstantExpr::getFPToSI(Constant *C, const Type *Ty) { |
Devang Patel | d26344d | 2008-11-03 23:20:04 +0000 | [diff] [blame] | 2150 | #ifndef NDEBUG |
Nate Begeman | d4d45c2 | 2007-11-17 03:58:34 +0000 | [diff] [blame] | 2151 | bool fromVec = C->getType()->getTypeID() == Type::VectorTyID; |
| 2152 | bool toVec = Ty->getTypeID() == Type::VectorTyID; |
Devang Patel | d26344d | 2008-11-03 23:20:04 +0000 | [diff] [blame] | 2153 | #endif |
Nate Begeman | d4d45c2 | 2007-11-17 03:58:34 +0000 | [diff] [blame] | 2154 | assert((fromVec == toVec) && "Cannot convert from scalar to/from vector"); |
| 2155 | assert(C->getType()->isFPOrFPVector() && Ty->isIntOrIntVector() && |
| 2156 | "This is an illegal floating point to sint cast!"); |
Owen Anderson | 0d2de8c | 2009-06-20 00:24:58 +0000 | [diff] [blame] | 2157 | return getFoldedCast(Instruction::FPToSI, C, Ty); |
Reid Spencer | 6c38f0b | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 2158 | } |
| 2159 | |
Owen Anderson | 0d2de8c | 2009-06-20 00:24:58 +0000 | [diff] [blame] | 2160 | Constant *ConstantExpr::getPtrToInt(Constant *C, const Type *DstTy) { |
Reid Spencer | 6c38f0b | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 2161 | assert(isa<PointerType>(C->getType()) && "PtrToInt source must be pointer"); |
Chris Lattner | 03c4953 | 2007-01-15 02:27:26 +0000 | [diff] [blame] | 2162 | assert(DstTy->isInteger() && "PtrToInt destination must be integral"); |
Owen Anderson | 0d2de8c | 2009-06-20 00:24:58 +0000 | [diff] [blame] | 2163 | return getFoldedCast(Instruction::PtrToInt, C, DstTy); |
Reid Spencer | 6c38f0b | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 2164 | } |
| 2165 | |
Owen Anderson | 0d2de8c | 2009-06-20 00:24:58 +0000 | [diff] [blame] | 2166 | Constant *ConstantExpr::getIntToPtr(Constant *C, const Type *DstTy) { |
Chris Lattner | 03c4953 | 2007-01-15 02:27:26 +0000 | [diff] [blame] | 2167 | assert(C->getType()->isInteger() && "IntToPtr source must be integral"); |
Reid Spencer | 6c38f0b | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 2168 | assert(isa<PointerType>(DstTy) && "IntToPtr destination must be a pointer"); |
Owen Anderson | 0d2de8c | 2009-06-20 00:24:58 +0000 | [diff] [blame] | 2169 | return getFoldedCast(Instruction::IntToPtr, C, DstTy); |
Reid Spencer | 6c38f0b | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 2170 | } |
| 2171 | |
Owen Anderson | 0d2de8c | 2009-06-20 00:24:58 +0000 | [diff] [blame] | 2172 | Constant *ConstantExpr::getBitCast(Constant *C, const Type *DstTy) { |
Reid Spencer | 6c38f0b | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 2173 | // BitCast implies a no-op cast of type only. No bits change. However, you |
| 2174 | // can't cast pointers to anything but pointers. |
Devang Patel | d26344d | 2008-11-03 23:20:04 +0000 | [diff] [blame] | 2175 | #ifndef NDEBUG |
Reid Spencer | 6c38f0b | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 2176 | const Type *SrcTy = C->getType(); |
| 2177 | assert((isa<PointerType>(SrcTy) == isa<PointerType>(DstTy)) && |
Reid Spencer | 5c14088 | 2006-12-04 20:17:56 +0000 | [diff] [blame] | 2178 | "BitCast cannot cast pointer to non-pointer and vice versa"); |
Reid Spencer | 6c38f0b | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 2179 | |
| 2180 | // Now we know we're not dealing with mismatched pointer casts (ptr->nonptr |
| 2181 | // or nonptr->ptr). For all the other types, the cast is okay if source and |
| 2182 | // destination bit widths are identical. |
| 2183 | unsigned SrcBitSize = SrcTy->getPrimitiveSizeInBits(); |
| 2184 | unsigned DstBitSize = DstTy->getPrimitiveSizeInBits(); |
Devang Patel | d26344d | 2008-11-03 23:20:04 +0000 | [diff] [blame] | 2185 | #endif |
Chris Lattner | e408601 | 2009-03-08 04:06:26 +0000 | [diff] [blame] | 2186 | assert(SrcBitSize == DstBitSize && "BitCast requires types of same width"); |
Chris Lattner | cbeda87 | 2009-03-21 06:55:54 +0000 | [diff] [blame] | 2187 | |
| 2188 | // It is common to ask for a bitcast of a value to its own type, handle this |
| 2189 | // speedily. |
| 2190 | if (C->getType() == DstTy) return C; |
| 2191 | |
Owen Anderson | 0d2de8c | 2009-06-20 00:24:58 +0000 | [diff] [blame] | 2192 | return getFoldedCast(Instruction::BitCast, C, DstTy); |
Chris Lattner | dd28474 | 2004-04-04 23:20:30 +0000 | [diff] [blame] | 2193 | } |
| 2194 | |
Chris Lattner | b50d135 | 2003-10-05 00:17:43 +0000 | [diff] [blame] | 2195 | Constant *ConstantExpr::getTy(const Type *ReqTy, unsigned Opcode, |
Owen Anderson | 0d2de8c | 2009-06-20 00:24:58 +0000 | [diff] [blame] | 2196 | Constant *C1, Constant *C2) { |
Chris Lattner | 38a9bcd | 2003-05-21 17:49:25 +0000 | [diff] [blame] | 2197 | // Check the operands for consistency first |
Reid Spencer | 7eb55b3 | 2006-11-02 01:53:59 +0000 | [diff] [blame] | 2198 | assert(Opcode >= Instruction::BinaryOpsBegin && |
| 2199 | Opcode < Instruction::BinaryOpsEnd && |
Chris Lattner | 38a9bcd | 2003-05-21 17:49:25 +0000 | [diff] [blame] | 2200 | "Invalid opcode in binary constant expression"); |
| 2201 | assert(C1->getType() == C2->getType() && |
| 2202 | "Operand types in binary constant expression should match"); |
Chris Lattner | b50d135 | 2003-10-05 00:17:43 +0000 | [diff] [blame] | 2203 | |
Reid Spencer | 542964f | 2007-01-11 18:21:29 +0000 | [diff] [blame] | 2204 | if (ReqTy == C1->getType() || ReqTy == Type::Int1Ty) |
Owen Anderson | 53a5221 | 2009-07-13 04:09:18 +0000 | [diff] [blame^] | 2205 | if (Constant *FC = ConstantFoldBinaryInstruction( |
| 2206 | getGlobalContext(), Opcode, C1, C2)) |
Chris Lattner | b50d135 | 2003-10-05 00:17:43 +0000 | [diff] [blame] | 2207 | return FC; // Fold a few common cases... |
Chris Lattner | acdbe71 | 2003-04-17 19:24:48 +0000 | [diff] [blame] | 2208 | |
Chris Lattner | 2b383d2e | 2003-05-13 21:37:02 +0000 | [diff] [blame] | 2209 | std::vector<Constant*> argVec(1, C1); argVec.push_back(C2); |
Reid Spencer | a009d0d | 2006-12-04 21:35:24 +0000 | [diff] [blame] | 2210 | ExprMapKeyType Key(Opcode, argVec); |
Owen Anderson | 6179404 | 2009-06-17 20:10:08 +0000 | [diff] [blame] | 2211 | |
Owen Anderson | 0d2de8c | 2009-06-20 00:24:58 +0000 | [diff] [blame] | 2212 | // Implicitly locked. |
| 2213 | return ExprConstants->getOrCreate(ReqTy, Key); |
Vikram S. Adve | 4e537b2 | 2002-07-14 23:13:17 +0000 | [diff] [blame] | 2214 | } |
| 2215 | |
Reid Spencer | 266e42b | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 2216 | Constant *ConstantExpr::getCompareTy(unsigned short predicate, |
Nate Begeman | 098cc6f | 2008-07-25 17:56:27 +0000 | [diff] [blame] | 2217 | Constant *C1, Constant *C2) { |
Reid Spencer | 266e42b | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 2218 | switch (predicate) { |
Torok Edwin | 56d0659 | 2009-07-11 20:10:48 +0000 | [diff] [blame] | 2219 | default: LLVM_UNREACHABLE("Invalid CmpInst predicate"); |
Nate Begeman | c96e2e4 | 2008-07-25 17:35:37 +0000 | [diff] [blame] | 2220 | case CmpInst::FCMP_FALSE: case CmpInst::FCMP_OEQ: case CmpInst::FCMP_OGT: |
| 2221 | case CmpInst::FCMP_OGE: case CmpInst::FCMP_OLT: case CmpInst::FCMP_OLE: |
| 2222 | case CmpInst::FCMP_ONE: case CmpInst::FCMP_ORD: case CmpInst::FCMP_UNO: |
| 2223 | case CmpInst::FCMP_UEQ: case CmpInst::FCMP_UGT: case CmpInst::FCMP_UGE: |
| 2224 | case CmpInst::FCMP_ULT: case CmpInst::FCMP_ULE: case CmpInst::FCMP_UNE: |
| 2225 | case CmpInst::FCMP_TRUE: |
Nick Lewycky | a21d3da | 2009-07-08 03:04:38 +0000 | [diff] [blame] | 2226 | return getFCmp(predicate, C1, C2); |
| 2227 | |
Nate Begeman | c96e2e4 | 2008-07-25 17:35:37 +0000 | [diff] [blame] | 2228 | case CmpInst::ICMP_EQ: case CmpInst::ICMP_NE: case CmpInst::ICMP_UGT: |
| 2229 | case CmpInst::ICMP_UGE: case CmpInst::ICMP_ULT: case CmpInst::ICMP_ULE: |
| 2230 | case CmpInst::ICMP_SGT: case CmpInst::ICMP_SGE: case CmpInst::ICMP_SLT: |
| 2231 | case CmpInst::ICMP_SLE: |
Nick Lewycky | a21d3da | 2009-07-08 03:04:38 +0000 | [diff] [blame] | 2232 | return getICmp(predicate, C1, C2); |
Reid Spencer | 266e42b | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 2233 | } |
Reid Spencer | a009d0d | 2006-12-04 21:35:24 +0000 | [diff] [blame] | 2234 | } |
| 2235 | |
Owen Anderson | 0d2de8c | 2009-06-20 00:24:58 +0000 | [diff] [blame] | 2236 | Constant *ConstantExpr::get(unsigned Opcode, Constant *C1, Constant *C2) { |
Dan Gohman | a5b9645 | 2009-06-04 22:49:04 +0000 | [diff] [blame] | 2237 | // API compatibility: Adjust integer opcodes to floating-point opcodes. |
| 2238 | if (C1->getType()->isFPOrFPVector()) { |
| 2239 | if (Opcode == Instruction::Add) Opcode = Instruction::FAdd; |
| 2240 | else if (Opcode == Instruction::Sub) Opcode = Instruction::FSub; |
| 2241 | else if (Opcode == Instruction::Mul) Opcode = Instruction::FMul; |
| 2242 | } |
Chris Lattner | caf3f3e | 2004-08-17 17:28:46 +0000 | [diff] [blame] | 2243 | #ifndef NDEBUG |
| 2244 | switch (Opcode) { |
Dan Gohman | a5b9645 | 2009-06-04 22:49:04 +0000 | [diff] [blame] | 2245 | case Instruction::Add: |
Reid Spencer | 7eb55b3 | 2006-11-02 01:53:59 +0000 | [diff] [blame] | 2246 | case Instruction::Sub: |
Dan Gohman | a5b9645 | 2009-06-04 22:49:04 +0000 | [diff] [blame] | 2247 | case Instruction::Mul: |
Chris Lattner | caf3f3e | 2004-08-17 17:28:46 +0000 | [diff] [blame] | 2248 | assert(C1->getType() == C2->getType() && "Op types should be identical!"); |
Dan Gohman | a5b9645 | 2009-06-04 22:49:04 +0000 | [diff] [blame] | 2249 | assert(C1->getType()->isIntOrIntVector() && |
| 2250 | "Tried to create an integer operation on a non-integer type!"); |
| 2251 | break; |
| 2252 | case Instruction::FAdd: |
| 2253 | case Instruction::FSub: |
| 2254 | case Instruction::FMul: |
| 2255 | assert(C1->getType() == C2->getType() && "Op types should be identical!"); |
| 2256 | assert(C1->getType()->isFPOrFPVector() && |
| 2257 | "Tried to create a floating-point operation on a " |
| 2258 | "non-floating-point type!"); |
Chris Lattner | caf3f3e | 2004-08-17 17:28:46 +0000 | [diff] [blame] | 2259 | break; |
Reid Spencer | 7e80b0b | 2006-10-26 06:15:43 +0000 | [diff] [blame] | 2260 | case Instruction::UDiv: |
| 2261 | case Instruction::SDiv: |
| 2262 | assert(C1->getType() == C2->getType() && "Op types should be identical!"); |
Dan Gohman | 7889f2b | 2009-06-15 22:25:12 +0000 | [diff] [blame] | 2263 | assert(C1->getType()->isIntOrIntVector() && |
Reid Spencer | 7e80b0b | 2006-10-26 06:15:43 +0000 | [diff] [blame] | 2264 | "Tried to create an arithmetic operation on a non-arithmetic type!"); |
| 2265 | break; |
| 2266 | case Instruction::FDiv: |
| 2267 | assert(C1->getType() == C2->getType() && "Op types should be identical!"); |
Dan Gohman | 7889f2b | 2009-06-15 22:25:12 +0000 | [diff] [blame] | 2268 | assert(C1->getType()->isFPOrFPVector() && |
| 2269 | "Tried to create an arithmetic operation on a non-arithmetic type!"); |
Reid Spencer | 7e80b0b | 2006-10-26 06:15:43 +0000 | [diff] [blame] | 2270 | break; |
Reid Spencer | 7eb55b3 | 2006-11-02 01:53:59 +0000 | [diff] [blame] | 2271 | case Instruction::URem: |
| 2272 | case Instruction::SRem: |
| 2273 | assert(C1->getType() == C2->getType() && "Op types should be identical!"); |
Dan Gohman | 7889f2b | 2009-06-15 22:25:12 +0000 | [diff] [blame] | 2274 | assert(C1->getType()->isIntOrIntVector() && |
Reid Spencer | 7eb55b3 | 2006-11-02 01:53:59 +0000 | [diff] [blame] | 2275 | "Tried to create an arithmetic operation on a non-arithmetic type!"); |
| 2276 | break; |
| 2277 | case Instruction::FRem: |
| 2278 | assert(C1->getType() == C2->getType() && "Op types should be identical!"); |
Dan Gohman | 7889f2b | 2009-06-15 22:25:12 +0000 | [diff] [blame] | 2279 | assert(C1->getType()->isFPOrFPVector() && |
| 2280 | "Tried to create an arithmetic operation on a non-arithmetic type!"); |
Reid Spencer | 7eb55b3 | 2006-11-02 01:53:59 +0000 | [diff] [blame] | 2281 | break; |
Chris Lattner | caf3f3e | 2004-08-17 17:28:46 +0000 | [diff] [blame] | 2282 | case Instruction::And: |
| 2283 | case Instruction::Or: |
| 2284 | case Instruction::Xor: |
| 2285 | assert(C1->getType() == C2->getType() && "Op types should be identical!"); |
Dan Gohman | 7889f2b | 2009-06-15 22:25:12 +0000 | [diff] [blame] | 2286 | assert(C1->getType()->isIntOrIntVector() && |
Misha Brukman | 3852f65 | 2005-01-27 06:46:38 +0000 | [diff] [blame] | 2287 | "Tried to create a logical operation on a non-integral type!"); |
Chris Lattner | caf3f3e | 2004-08-17 17:28:46 +0000 | [diff] [blame] | 2288 | break; |
Chris Lattner | caf3f3e | 2004-08-17 17:28:46 +0000 | [diff] [blame] | 2289 | case Instruction::Shl: |
Reid Spencer | fdff938 | 2006-11-08 06:47:33 +0000 | [diff] [blame] | 2290 | case Instruction::LShr: |
| 2291 | case Instruction::AShr: |
Reid Spencer | 2341c22 | 2007-02-02 02:16:23 +0000 | [diff] [blame] | 2292 | assert(C1->getType() == C2->getType() && "Op types should be identical!"); |
Dan Gohman | 79975d5 | 2009-03-14 17:09:17 +0000 | [diff] [blame] | 2293 | assert(C1->getType()->isIntOrIntVector() && |
Chris Lattner | caf3f3e | 2004-08-17 17:28:46 +0000 | [diff] [blame] | 2294 | "Tried to create a shift operation on a non-integer type!"); |
| 2295 | break; |
| 2296 | default: |
| 2297 | break; |
| 2298 | } |
| 2299 | #endif |
| 2300 | |
Owen Anderson | 0d2de8c | 2009-06-20 00:24:58 +0000 | [diff] [blame] | 2301 | return getTy(C1->getType(), Opcode, C1, C2); |
Reid Spencer | a009d0d | 2006-12-04 21:35:24 +0000 | [diff] [blame] | 2302 | } |
| 2303 | |
Reid Spencer | 266e42b | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 2304 | Constant *ConstantExpr::getCompare(unsigned short pred, |
Reid Spencer | a009d0d | 2006-12-04 21:35:24 +0000 | [diff] [blame] | 2305 | Constant *C1, Constant *C2) { |
| 2306 | assert(C1->getType() == C2->getType() && "Op types should be identical!"); |
Reid Spencer | 266e42b | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 2307 | return getCompareTy(pred, C1, C2); |
Chris Lattner | 29ca2c6 | 2004-08-04 18:50:09 +0000 | [diff] [blame] | 2308 | } |
| 2309 | |
Chris Lattner | 6e415c0 | 2004-03-12 05:54:04 +0000 | [diff] [blame] | 2310 | Constant *ConstantExpr::getSelectTy(const Type *ReqTy, Constant *C, |
Owen Anderson | 0d2de8c | 2009-06-20 00:24:58 +0000 | [diff] [blame] | 2311 | Constant *V1, Constant *V2) { |
Chris Lattner | 4163213 | 2008-12-29 00:16:12 +0000 | [diff] [blame] | 2312 | assert(!SelectInst::areInvalidOperands(C, V1, V2)&&"Invalid select operands"); |
Chris Lattner | 6e415c0 | 2004-03-12 05:54:04 +0000 | [diff] [blame] | 2313 | |
| 2314 | if (ReqTy == V1->getType()) |
Owen Anderson | 53a5221 | 2009-07-13 04:09:18 +0000 | [diff] [blame^] | 2315 | if (Constant *SC = ConstantFoldSelectInstruction( |
| 2316 | getGlobalContext(), C, V1, V2)) |
Chris Lattner | 6e415c0 | 2004-03-12 05:54:04 +0000 | [diff] [blame] | 2317 | return SC; // Fold common cases |
| 2318 | |
| 2319 | std::vector<Constant*> argVec(3, C); |
| 2320 | argVec[1] = V1; |
| 2321 | argVec[2] = V2; |
Reid Spencer | ee3c991 | 2006-12-04 05:19:50 +0000 | [diff] [blame] | 2322 | ExprMapKeyType Key(Instruction::Select, argVec); |
Owen Anderson | 6179404 | 2009-06-17 20:10:08 +0000 | [diff] [blame] | 2323 | |
Owen Anderson | 0d2de8c | 2009-06-20 00:24:58 +0000 | [diff] [blame] | 2324 | // Implicitly locked. |
| 2325 | return ExprConstants->getOrCreate(ReqTy, Key); |
Chris Lattner | 6e415c0 | 2004-03-12 05:54:04 +0000 | [diff] [blame] | 2326 | } |
| 2327 | |
Chris Lattner | b50d135 | 2003-10-05 00:17:43 +0000 | [diff] [blame] | 2328 | Constant *ConstantExpr::getGetElementPtrTy(const Type *ReqTy, Constant *C, |
Chris Lattner | 302116a | 2007-01-31 04:40:28 +0000 | [diff] [blame] | 2329 | Value* const *Idxs, |
Owen Anderson | 0d2de8c | 2009-06-20 00:24:58 +0000 | [diff] [blame] | 2330 | unsigned NumIdx) { |
Dan Gohman | 12fce77 | 2008-05-15 19:50:34 +0000 | [diff] [blame] | 2331 | assert(GetElementPtrInst::getIndexedType(C->getType(), Idxs, |
| 2332 | Idxs+NumIdx) == |
| 2333 | cast<PointerType>(ReqTy)->getElementType() && |
| 2334 | "GEP indices invalid!"); |
Chris Lattner | 04b60fe | 2004-02-16 20:46:13 +0000 | [diff] [blame] | 2335 | |
Owen Anderson | 53a5221 | 2009-07-13 04:09:18 +0000 | [diff] [blame^] | 2336 | if (Constant *FC = ConstantFoldGetElementPtr( |
| 2337 | getGlobalContext(), C, (Constant**)Idxs, NumIdx)) |
Chris Lattner | acdbe71 | 2003-04-17 19:24:48 +0000 | [diff] [blame] | 2338 | return FC; // Fold a few common cases... |
Chris Lattner | 04b60fe | 2004-02-16 20:46:13 +0000 | [diff] [blame] | 2339 | |
Chris Lattner | b50d135 | 2003-10-05 00:17:43 +0000 | [diff] [blame] | 2340 | assert(isa<PointerType>(C->getType()) && |
Chris Lattner | 98fa07b | 2003-05-23 20:03:32 +0000 | [diff] [blame] | 2341 | "Non-pointer type for constant GetElementPtr expression"); |
Vikram S. Adve | 4c48533 | 2002-07-15 18:19:33 +0000 | [diff] [blame] | 2342 | // Look up the constant in the table first to ensure uniqueness |
Chris Lattner | 13128ab | 2004-10-11 22:52:25 +0000 | [diff] [blame] | 2343 | std::vector<Constant*> ArgVec; |
Chris Lattner | 302116a | 2007-01-31 04:40:28 +0000 | [diff] [blame] | 2344 | ArgVec.reserve(NumIdx+1); |
Chris Lattner | 13128ab | 2004-10-11 22:52:25 +0000 | [diff] [blame] | 2345 | ArgVec.push_back(C); |
Chris Lattner | 302116a | 2007-01-31 04:40:28 +0000 | [diff] [blame] | 2346 | for (unsigned i = 0; i != NumIdx; ++i) |
| 2347 | ArgVec.push_back(cast<Constant>(Idxs[i])); |
| 2348 | const ExprMapKeyType Key(Instruction::GetElementPtr, ArgVec); |
Owen Anderson | 6179404 | 2009-06-17 20:10:08 +0000 | [diff] [blame] | 2349 | |
| 2350 | // Implicitly locked. |
Owen Anderson | 0d2de8c | 2009-06-20 00:24:58 +0000 | [diff] [blame] | 2351 | return ExprConstants->getOrCreate(ReqTy, Key); |
Vikram S. Adve | 4c48533 | 2002-07-15 18:19:33 +0000 | [diff] [blame] | 2352 | } |
| 2353 | |
Chris Lattner | 302116a | 2007-01-31 04:40:28 +0000 | [diff] [blame] | 2354 | Constant *ConstantExpr::getGetElementPtr(Constant *C, Value* const *Idxs, |
Owen Anderson | 0d2de8c | 2009-06-20 00:24:58 +0000 | [diff] [blame] | 2355 | unsigned NumIdx) { |
Chris Lattner | b50d135 | 2003-10-05 00:17:43 +0000 | [diff] [blame] | 2356 | // Get the result type of the getelementptr! |
Chris Lattner | 302116a | 2007-01-31 04:40:28 +0000 | [diff] [blame] | 2357 | const Type *Ty = |
Dan Gohman | 12fce77 | 2008-05-15 19:50:34 +0000 | [diff] [blame] | 2358 | GetElementPtrInst::getIndexedType(C->getType(), Idxs, Idxs+NumIdx); |
Chris Lattner | b50d135 | 2003-10-05 00:17:43 +0000 | [diff] [blame] | 2359 | assert(Ty && "GEP indices invalid!"); |
Christopher Lamb | 54dd24c | 2007-12-11 08:59:05 +0000 | [diff] [blame] | 2360 | unsigned As = cast<PointerType>(C->getType())->getAddressSpace(); |
Owen Anderson | 0d2de8c | 2009-06-20 00:24:58 +0000 | [diff] [blame] | 2361 | return getGetElementPtrTy(PointerType::get(Ty, As), C, Idxs, NumIdx); |
Chris Lattner | 13128ab | 2004-10-11 22:52:25 +0000 | [diff] [blame] | 2362 | } |
| 2363 | |
Chris Lattner | 302116a | 2007-01-31 04:40:28 +0000 | [diff] [blame] | 2364 | Constant *ConstantExpr::getGetElementPtr(Constant *C, Constant* const *Idxs, |
Owen Anderson | 0d2de8c | 2009-06-20 00:24:58 +0000 | [diff] [blame] | 2365 | unsigned NumIdx) { |
| 2366 | return getGetElementPtr(C, (Value* const *)Idxs, NumIdx); |
Chris Lattner | b50d135 | 2003-10-05 00:17:43 +0000 | [diff] [blame] | 2367 | } |
| 2368 | |
Chris Lattner | 302116a | 2007-01-31 04:40:28 +0000 | [diff] [blame] | 2369 | |
Reid Spencer | ee3c991 | 2006-12-04 05:19:50 +0000 | [diff] [blame] | 2370 | Constant * |
| 2371 | ConstantExpr::getICmp(unsigned short pred, Constant* LHS, Constant* RHS) { |
| 2372 | assert(LHS->getType() == RHS->getType()); |
| 2373 | assert(pred >= ICmpInst::FIRST_ICMP_PREDICATE && |
| 2374 | pred <= ICmpInst::LAST_ICMP_PREDICATE && "Invalid ICmp Predicate"); |
| 2375 | |
Owen Anderson | 53a5221 | 2009-07-13 04:09:18 +0000 | [diff] [blame^] | 2376 | if (Constant *FC = ConstantFoldCompareInstruction( |
| 2377 | getGlobalContext(),pred, LHS, RHS)) |
Reid Spencer | ee3c991 | 2006-12-04 05:19:50 +0000 | [diff] [blame] | 2378 | return FC; // Fold a few common cases... |
| 2379 | |
| 2380 | // Look up the constant in the table first to ensure uniqueness |
| 2381 | std::vector<Constant*> ArgVec; |
| 2382 | ArgVec.push_back(LHS); |
| 2383 | ArgVec.push_back(RHS); |
Reid Spencer | b153749 | 2006-12-24 18:42:29 +0000 | [diff] [blame] | 2384 | // Get the key type with both the opcode and predicate |
Reid Spencer | ee3c991 | 2006-12-04 05:19:50 +0000 | [diff] [blame] | 2385 | const ExprMapKeyType Key(Instruction::ICmp, ArgVec, pred); |
Owen Anderson | 6179404 | 2009-06-17 20:10:08 +0000 | [diff] [blame] | 2386 | |
| 2387 | // Implicitly locked. |
| 2388 | return ExprConstants->getOrCreate(Type::Int1Ty, Key); |
Reid Spencer | ee3c991 | 2006-12-04 05:19:50 +0000 | [diff] [blame] | 2389 | } |
| 2390 | |
| 2391 | Constant * |
| 2392 | ConstantExpr::getFCmp(unsigned short pred, Constant* LHS, Constant* RHS) { |
| 2393 | assert(LHS->getType() == RHS->getType()); |
| 2394 | assert(pred <= FCmpInst::LAST_FCMP_PREDICATE && "Invalid FCmp Predicate"); |
| 2395 | |
Owen Anderson | 53a5221 | 2009-07-13 04:09:18 +0000 | [diff] [blame^] | 2396 | if (Constant *FC = ConstantFoldCompareInstruction( |
| 2397 | getGlobalContext(), pred, LHS, RHS)) |
Reid Spencer | ee3c991 | 2006-12-04 05:19:50 +0000 | [diff] [blame] | 2398 | return FC; // Fold a few common cases... |
| 2399 | |
| 2400 | // Look up the constant in the table first to ensure uniqueness |
| 2401 | std::vector<Constant*> ArgVec; |
| 2402 | ArgVec.push_back(LHS); |
| 2403 | ArgVec.push_back(RHS); |
Reid Spencer | b153749 | 2006-12-24 18:42:29 +0000 | [diff] [blame] | 2404 | // Get the key type with both the opcode and predicate |
Reid Spencer | ee3c991 | 2006-12-04 05:19:50 +0000 | [diff] [blame] | 2405 | const ExprMapKeyType Key(Instruction::FCmp, ArgVec, pred); |
Owen Anderson | 6179404 | 2009-06-17 20:10:08 +0000 | [diff] [blame] | 2406 | |
| 2407 | // Implicitly locked. |
| 2408 | return ExprConstants->getOrCreate(Type::Int1Ty, Key); |
Reid Spencer | ee3c991 | 2006-12-04 05:19:50 +0000 | [diff] [blame] | 2409 | } |
| 2410 | |
Robert Bocchino | 2300448 | 2006-01-10 19:05:34 +0000 | [diff] [blame] | 2411 | Constant *ConstantExpr::getExtractElementTy(const Type *ReqTy, Constant *Val, |
| 2412 | Constant *Idx) { |
Owen Anderson | 53a5221 | 2009-07-13 04:09:18 +0000 | [diff] [blame^] | 2413 | if (Constant *FC = ConstantFoldExtractElementInstruction( |
| 2414 | getGlobalContext(), Val, Idx)) |
Robert Bocchino | de7f1c9 | 2006-01-10 20:03:46 +0000 | [diff] [blame] | 2415 | return FC; // Fold a few common cases... |
Robert Bocchino | 2300448 | 2006-01-10 19:05:34 +0000 | [diff] [blame] | 2416 | // Look up the constant in the table first to ensure uniqueness |
| 2417 | std::vector<Constant*> ArgVec(1, Val); |
| 2418 | ArgVec.push_back(Idx); |
Reid Spencer | ee3c991 | 2006-12-04 05:19:50 +0000 | [diff] [blame] | 2419 | const ExprMapKeyType Key(Instruction::ExtractElement,ArgVec); |
Owen Anderson | 6179404 | 2009-06-17 20:10:08 +0000 | [diff] [blame] | 2420 | |
| 2421 | // Implicitly locked. |
| 2422 | return ExprConstants->getOrCreate(ReqTy, Key); |
Robert Bocchino | 2300448 | 2006-01-10 19:05:34 +0000 | [diff] [blame] | 2423 | } |
| 2424 | |
| 2425 | Constant *ConstantExpr::getExtractElement(Constant *Val, Constant *Idx) { |
Reid Spencer | d84d35b | 2007-02-15 02:26:10 +0000 | [diff] [blame] | 2426 | assert(isa<VectorType>(Val->getType()) && |
Reid Spencer | 09575ba | 2007-02-15 03:39:18 +0000 | [diff] [blame] | 2427 | "Tried to create extractelement operation on non-vector type!"); |
Reid Spencer | 8d9336d | 2006-12-31 05:26:44 +0000 | [diff] [blame] | 2428 | assert(Idx->getType() == Type::Int32Ty && |
Reid Spencer | 2546b76 | 2007-01-26 07:37:34 +0000 | [diff] [blame] | 2429 | "Extractelement index must be i32 type!"); |
Reid Spencer | d84d35b | 2007-02-15 02:26:10 +0000 | [diff] [blame] | 2430 | return getExtractElementTy(cast<VectorType>(Val->getType())->getElementType(), |
Robert Bocchino | 2300448 | 2006-01-10 19:05:34 +0000 | [diff] [blame] | 2431 | Val, Idx); |
| 2432 | } |
Chris Lattner | b50d135 | 2003-10-05 00:17:43 +0000 | [diff] [blame] | 2433 | |
Robert Bocchino | ca27f03 | 2006-01-17 20:07:22 +0000 | [diff] [blame] | 2434 | Constant *ConstantExpr::getInsertElementTy(const Type *ReqTy, Constant *Val, |
| 2435 | Constant *Elt, Constant *Idx) { |
Owen Anderson | 53a5221 | 2009-07-13 04:09:18 +0000 | [diff] [blame^] | 2436 | if (Constant *FC = ConstantFoldInsertElementInstruction( |
| 2437 | getGlobalContext(), Val, Elt, Idx)) |
Robert Bocchino | ca27f03 | 2006-01-17 20:07:22 +0000 | [diff] [blame] | 2438 | return FC; // Fold a few common cases... |
| 2439 | // Look up the constant in the table first to ensure uniqueness |
| 2440 | std::vector<Constant*> ArgVec(1, Val); |
| 2441 | ArgVec.push_back(Elt); |
| 2442 | ArgVec.push_back(Idx); |
Reid Spencer | ee3c991 | 2006-12-04 05:19:50 +0000 | [diff] [blame] | 2443 | const ExprMapKeyType Key(Instruction::InsertElement,ArgVec); |
Owen Anderson | 6179404 | 2009-06-17 20:10:08 +0000 | [diff] [blame] | 2444 | |
| 2445 | // Implicitly locked. |
| 2446 | return ExprConstants->getOrCreate(ReqTy, Key); |
Robert Bocchino | ca27f03 | 2006-01-17 20:07:22 +0000 | [diff] [blame] | 2447 | } |
| 2448 | |
| 2449 | Constant *ConstantExpr::getInsertElement(Constant *Val, Constant *Elt, |
| 2450 | Constant *Idx) { |
Reid Spencer | d84d35b | 2007-02-15 02:26:10 +0000 | [diff] [blame] | 2451 | assert(isa<VectorType>(Val->getType()) && |
Reid Spencer | 09575ba | 2007-02-15 03:39:18 +0000 | [diff] [blame] | 2452 | "Tried to create insertelement operation on non-vector type!"); |
Reid Spencer | d84d35b | 2007-02-15 02:26:10 +0000 | [diff] [blame] | 2453 | assert(Elt->getType() == cast<VectorType>(Val->getType())->getElementType() |
Robert Bocchino | ca27f03 | 2006-01-17 20:07:22 +0000 | [diff] [blame] | 2454 | && "Insertelement types must match!"); |
Reid Spencer | 8d9336d | 2006-12-31 05:26:44 +0000 | [diff] [blame] | 2455 | assert(Idx->getType() == Type::Int32Ty && |
Reid Spencer | 2546b76 | 2007-01-26 07:37:34 +0000 | [diff] [blame] | 2456 | "Insertelement index must be i32 type!"); |
Gordon Henriksen | b52d1ed | 2008-08-30 15:41:51 +0000 | [diff] [blame] | 2457 | return getInsertElementTy(Val->getType(), Val, Elt, Idx); |
Robert Bocchino | ca27f03 | 2006-01-17 20:07:22 +0000 | [diff] [blame] | 2458 | } |
| 2459 | |
Chris Lattner | bbe0a42 | 2006-04-08 01:18:18 +0000 | [diff] [blame] | 2460 | Constant *ConstantExpr::getShuffleVectorTy(const Type *ReqTy, Constant *V1, |
| 2461 | Constant *V2, Constant *Mask) { |
Owen Anderson | 53a5221 | 2009-07-13 04:09:18 +0000 | [diff] [blame^] | 2462 | if (Constant *FC = ConstantFoldShuffleVectorInstruction( |
| 2463 | getGlobalContext(), V1, V2, Mask)) |
Chris Lattner | bbe0a42 | 2006-04-08 01:18:18 +0000 | [diff] [blame] | 2464 | return FC; // Fold a few common cases... |
| 2465 | // Look up the constant in the table first to ensure uniqueness |
| 2466 | std::vector<Constant*> ArgVec(1, V1); |
| 2467 | ArgVec.push_back(V2); |
| 2468 | ArgVec.push_back(Mask); |
Reid Spencer | ee3c991 | 2006-12-04 05:19:50 +0000 | [diff] [blame] | 2469 | const ExprMapKeyType Key(Instruction::ShuffleVector,ArgVec); |
Owen Anderson | 6179404 | 2009-06-17 20:10:08 +0000 | [diff] [blame] | 2470 | |
| 2471 | // Implicitly locked. |
| 2472 | return ExprConstants->getOrCreate(ReqTy, Key); |
Chris Lattner | bbe0a42 | 2006-04-08 01:18:18 +0000 | [diff] [blame] | 2473 | } |
| 2474 | |
| 2475 | Constant *ConstantExpr::getShuffleVector(Constant *V1, Constant *V2, |
| 2476 | Constant *Mask) { |
| 2477 | assert(ShuffleVectorInst::isValidOperands(V1, V2, Mask) && |
| 2478 | "Invalid shuffle vector constant expr operands!"); |
Nate Begeman | 94aa38d | 2009-02-12 21:28:33 +0000 | [diff] [blame] | 2479 | |
| 2480 | unsigned NElts = cast<VectorType>(Mask->getType())->getNumElements(); |
| 2481 | const Type *EltTy = cast<VectorType>(V1->getType())->getElementType(); |
| 2482 | const Type *ShufTy = VectorType::get(EltTy, NElts); |
| 2483 | return getShuffleVectorTy(ShufTy, V1, V2, Mask); |
Chris Lattner | bbe0a42 | 2006-04-08 01:18:18 +0000 | [diff] [blame] | 2484 | } |
| 2485 | |
Dan Gohman | 12fce77 | 2008-05-15 19:50:34 +0000 | [diff] [blame] | 2486 | Constant *ConstantExpr::getInsertValueTy(const Type *ReqTy, Constant *Agg, |
| 2487 | Constant *Val, |
Dan Gohman | 1ecaf45 | 2008-05-31 00:58:22 +0000 | [diff] [blame] | 2488 | const unsigned *Idxs, unsigned NumIdx) { |
Dan Gohman | 12fce77 | 2008-05-15 19:50:34 +0000 | [diff] [blame] | 2489 | assert(ExtractValueInst::getIndexedType(Agg->getType(), Idxs, |
| 2490 | Idxs+NumIdx) == Val->getType() && |
| 2491 | "insertvalue indices invalid!"); |
| 2492 | assert(Agg->getType() == ReqTy && |
| 2493 | "insertvalue type invalid!"); |
Dan Gohman | 0752bff | 2008-05-23 00:36:11 +0000 | [diff] [blame] | 2494 | assert(Agg->getType()->isFirstClassType() && |
| 2495 | "Non-first-class type for constant InsertValue expression"); |
Owen Anderson | 53a5221 | 2009-07-13 04:09:18 +0000 | [diff] [blame^] | 2496 | Constant *FC = ConstantFoldInsertValueInstruction( |
| 2497 | getGlobalContext(), Agg, Val, Idxs, NumIdx); |
Dan Gohman | d5d24f6 | 2008-07-21 23:30:30 +0000 | [diff] [blame] | 2498 | assert(FC && "InsertValue constant expr couldn't be folded!"); |
| 2499 | return FC; |
Dan Gohman | 12fce77 | 2008-05-15 19:50:34 +0000 | [diff] [blame] | 2500 | } |
| 2501 | |
| 2502 | Constant *ConstantExpr::getInsertValue(Constant *Agg, Constant *Val, |
Dan Gohman | 1ecaf45 | 2008-05-31 00:58:22 +0000 | [diff] [blame] | 2503 | const unsigned *IdxList, unsigned NumIdx) { |
Dan Gohman | 0752bff | 2008-05-23 00:36:11 +0000 | [diff] [blame] | 2504 | assert(Agg->getType()->isFirstClassType() && |
| 2505 | "Tried to create insertelement operation on non-first-class type!"); |
Dan Gohman | 12fce77 | 2008-05-15 19:50:34 +0000 | [diff] [blame] | 2506 | |
Dan Gohman | 0752bff | 2008-05-23 00:36:11 +0000 | [diff] [blame] | 2507 | const Type *ReqTy = Agg->getType(); |
Devang Patel | d26344d | 2008-11-03 23:20:04 +0000 | [diff] [blame] | 2508 | #ifndef NDEBUG |
Dan Gohman | 0752bff | 2008-05-23 00:36:11 +0000 | [diff] [blame] | 2509 | const Type *ValTy = |
Dan Gohman | 12fce77 | 2008-05-15 19:50:34 +0000 | [diff] [blame] | 2510 | ExtractValueInst::getIndexedType(Agg->getType(), IdxList, IdxList+NumIdx); |
Devang Patel | d26344d | 2008-11-03 23:20:04 +0000 | [diff] [blame] | 2511 | #endif |
Dan Gohman | 0752bff | 2008-05-23 00:36:11 +0000 | [diff] [blame] | 2512 | assert(ValTy == Val->getType() && "insertvalue indices invalid!"); |
Dan Gohman | 12fce77 | 2008-05-15 19:50:34 +0000 | [diff] [blame] | 2513 | return getInsertValueTy(ReqTy, Agg, Val, IdxList, NumIdx); |
| 2514 | } |
| 2515 | |
| 2516 | Constant *ConstantExpr::getExtractValueTy(const Type *ReqTy, Constant *Agg, |
Dan Gohman | 1ecaf45 | 2008-05-31 00:58:22 +0000 | [diff] [blame] | 2517 | const unsigned *Idxs, unsigned NumIdx) { |
Dan Gohman | 12fce77 | 2008-05-15 19:50:34 +0000 | [diff] [blame] | 2518 | assert(ExtractValueInst::getIndexedType(Agg->getType(), Idxs, |
| 2519 | Idxs+NumIdx) == ReqTy && |
| 2520 | "extractvalue indices invalid!"); |
Dan Gohman | 0752bff | 2008-05-23 00:36:11 +0000 | [diff] [blame] | 2521 | assert(Agg->getType()->isFirstClassType() && |
| 2522 | "Non-first-class type for constant extractvalue expression"); |
Owen Anderson | 53a5221 | 2009-07-13 04:09:18 +0000 | [diff] [blame^] | 2523 | Constant *FC = ConstantFoldExtractValueInstruction( |
| 2524 | getGlobalContext(), Agg, Idxs, NumIdx); |
Dan Gohman | d5d24f6 | 2008-07-21 23:30:30 +0000 | [diff] [blame] | 2525 | assert(FC && "ExtractValue constant expr couldn't be folded!"); |
| 2526 | return FC; |
Dan Gohman | 12fce77 | 2008-05-15 19:50:34 +0000 | [diff] [blame] | 2527 | } |
| 2528 | |
| 2529 | Constant *ConstantExpr::getExtractValue(Constant *Agg, |
Dan Gohman | 1ecaf45 | 2008-05-31 00:58:22 +0000 | [diff] [blame] | 2530 | const unsigned *IdxList, unsigned NumIdx) { |
Dan Gohman | 0752bff | 2008-05-23 00:36:11 +0000 | [diff] [blame] | 2531 | assert(Agg->getType()->isFirstClassType() && |
| 2532 | "Tried to create extractelement operation on non-first-class type!"); |
Dan Gohman | 12fce77 | 2008-05-15 19:50:34 +0000 | [diff] [blame] | 2533 | |
| 2534 | const Type *ReqTy = |
| 2535 | ExtractValueInst::getIndexedType(Agg->getType(), IdxList, IdxList+NumIdx); |
| 2536 | assert(ReqTy && "extractvalue indices invalid!"); |
| 2537 | return getExtractValueTy(ReqTy, Agg, IdxList, NumIdx); |
| 2538 | } |
| 2539 | |
Vikram S. Adve | 4c48533 | 2002-07-15 18:19:33 +0000 | [diff] [blame] | 2540 | // destroyConstant - Remove the constant from the constant table... |
| 2541 | // |
Owen Anderson | 0d2de8c | 2009-06-20 00:24:58 +0000 | [diff] [blame] | 2542 | void ConstantExpr::destroyConstant() { |
| 2543 | // Implicitly locked. |
| 2544 | ExprConstants->remove(this); |
Vikram S. Adve | 4c48533 | 2002-07-15 18:19:33 +0000 | [diff] [blame] | 2545 | destroyConstantImpl(); |
Vikram S. Adve | 4e537b2 | 2002-07-14 23:13:17 +0000 | [diff] [blame] | 2546 | } |
| 2547 | |
Chris Lattner | 3cd8c56 | 2002-07-30 18:54:25 +0000 | [diff] [blame] | 2548 | const char *ConstantExpr::getOpcodeName() const { |
| 2549 | return Instruction::getOpcodeName(getOpcode()); |
Vikram S. Adve | 4e537b2 | 2002-07-14 23:13:17 +0000 | [diff] [blame] | 2550 | } |
Reid Spencer | 1ebe1ab | 2004-07-17 23:48:33 +0000 | [diff] [blame] | 2551 | |
Chris Lattner | c4062ba | 2005-10-03 21:58:36 +0000 | [diff] [blame] | 2552 | //===----------------------------------------------------------------------===// |
| 2553 | // replaceUsesOfWithOnConstant implementations |
| 2554 | |
Chris Lattner | 913849b | 2007-08-21 00:55:23 +0000 | [diff] [blame] | 2555 | /// replaceUsesOfWithOnConstant - Update this constant array to change uses of |
| 2556 | /// 'From' to be uses of 'To'. This must update the uniquing data structures |
| 2557 | /// etc. |
| 2558 | /// |
| 2559 | /// Note that we intentionally replace all uses of From with To here. Consider |
| 2560 | /// a large array that uses 'From' 1000 times. By handling this case all here, |
| 2561 | /// ConstantArray::replaceUsesOfWithOnConstant is only invoked once, and that |
| 2562 | /// single invocation handles all 1000 uses. Handling them one at a time would |
| 2563 | /// work, but would be really slow because it would have to unique each updated |
| 2564 | /// array instance. |
Chris Lattner | c4062ba | 2005-10-03 21:58:36 +0000 | [diff] [blame] | 2565 | void ConstantArray::replaceUsesOfWithOnConstant(Value *From, Value *To, |
Chris Lattner | 7a1450d | 2005-10-04 18:13:04 +0000 | [diff] [blame] | 2566 | Use *U) { |
Chris Lattner | c4062ba | 2005-10-03 21:58:36 +0000 | [diff] [blame] | 2567 | assert(isa<Constant>(To) && "Cannot make Constant refer to non-constant!"); |
Chris Lattner | 8760ec7 | 2005-10-04 01:17:50 +0000 | [diff] [blame] | 2568 | Constant *ToC = cast<Constant>(To); |
Chris Lattner | dff5911 | 2005-10-04 18:47:09 +0000 | [diff] [blame] | 2569 | |
Jim Laskey | c03caef | 2006-07-17 17:38:29 +0000 | [diff] [blame] | 2570 | std::pair<ArrayConstantsTy::MapKey, Constant*> Lookup; |
Chris Lattner | b64419a | 2005-10-03 22:51:37 +0000 | [diff] [blame] | 2571 | Lookup.first.first = getType(); |
| 2572 | Lookup.second = this; |
Chris Lattner | dff5911 | 2005-10-04 18:47:09 +0000 | [diff] [blame] | 2573 | |
Chris Lattner | b64419a | 2005-10-03 22:51:37 +0000 | [diff] [blame] | 2574 | std::vector<Constant*> &Values = Lookup.first.second; |
| 2575 | Values.reserve(getNumOperands()); // Build replacement array. |
Chris Lattner | dff5911 | 2005-10-04 18:47:09 +0000 | [diff] [blame] | 2576 | |
Chris Lattner | 8760ec7 | 2005-10-04 01:17:50 +0000 | [diff] [blame] | 2577 | // Fill values with the modified operands of the constant array. Also, |
| 2578 | // compute whether this turns into an all-zeros array. |
Chris Lattner | dff5911 | 2005-10-04 18:47:09 +0000 | [diff] [blame] | 2579 | bool isAllZeros = false; |
Chris Lattner | 913849b | 2007-08-21 00:55:23 +0000 | [diff] [blame] | 2580 | unsigned NumUpdated = 0; |
Chris Lattner | dff5911 | 2005-10-04 18:47:09 +0000 | [diff] [blame] | 2581 | if (!ToC->isNullValue()) { |
Chris Lattner | 913849b | 2007-08-21 00:55:23 +0000 | [diff] [blame] | 2582 | for (Use *O = OperandList, *E = OperandList+getNumOperands(); O != E; ++O) { |
| 2583 | Constant *Val = cast<Constant>(O->get()); |
| 2584 | if (Val == From) { |
| 2585 | Val = ToC; |
| 2586 | ++NumUpdated; |
| 2587 | } |
| 2588 | Values.push_back(Val); |
| 2589 | } |
Chris Lattner | dff5911 | 2005-10-04 18:47:09 +0000 | [diff] [blame] | 2590 | } else { |
| 2591 | isAllZeros = true; |
| 2592 | for (Use *O = OperandList, *E = OperandList+getNumOperands(); O != E; ++O) { |
| 2593 | Constant *Val = cast<Constant>(O->get()); |
Chris Lattner | 913849b | 2007-08-21 00:55:23 +0000 | [diff] [blame] | 2594 | if (Val == From) { |
| 2595 | Val = ToC; |
| 2596 | ++NumUpdated; |
| 2597 | } |
Chris Lattner | dff5911 | 2005-10-04 18:47:09 +0000 | [diff] [blame] | 2598 | Values.push_back(Val); |
| 2599 | if (isAllZeros) isAllZeros = Val->isNullValue(); |
| 2600 | } |
Chris Lattner | c4062ba | 2005-10-03 21:58:36 +0000 | [diff] [blame] | 2601 | } |
| 2602 | |
Chris Lattner | b64419a | 2005-10-03 22:51:37 +0000 | [diff] [blame] | 2603 | Constant *Replacement = 0; |
| 2604 | if (isAllZeros) { |
| 2605 | Replacement = ConstantAggregateZero::get(getType()); |
| 2606 | } else { |
| 2607 | // Check to see if we have this array type already. |
Owen Anderson | 5c96ef7 | 2009-07-07 18:33:04 +0000 | [diff] [blame] | 2608 | sys::SmartScopedWriter<true> Writer(*ConstantsLock); |
Chris Lattner | b64419a | 2005-10-03 22:51:37 +0000 | [diff] [blame] | 2609 | bool Exists; |
Jim Laskey | c03caef | 2006-07-17 17:38:29 +0000 | [diff] [blame] | 2610 | ArrayConstantsTy::MapTy::iterator I = |
Chris Lattner | 69edc98 | 2006-09-28 00:35:06 +0000 | [diff] [blame] | 2611 | ArrayConstants->InsertOrGetItem(Lookup, Exists); |
Chris Lattner | b64419a | 2005-10-03 22:51:37 +0000 | [diff] [blame] | 2612 | |
| 2613 | if (Exists) { |
| 2614 | Replacement = I->second; |
| 2615 | } else { |
| 2616 | // Okay, the new shape doesn't exist in the system yet. Instead of |
| 2617 | // creating a new constant array, inserting it, replaceallusesof'ing the |
| 2618 | // old with the new, then deleting the old... just update the current one |
| 2619 | // in place! |
Chris Lattner | 69edc98 | 2006-09-28 00:35:06 +0000 | [diff] [blame] | 2620 | ArrayConstants->MoveConstantToNewSlot(this, I); |
Chris Lattner | b64419a | 2005-10-03 22:51:37 +0000 | [diff] [blame] | 2621 | |
Chris Lattner | 913849b | 2007-08-21 00:55:23 +0000 | [diff] [blame] | 2622 | // Update to the new value. Optimize for the case when we have a single |
| 2623 | // operand that we're changing, but handle bulk updates efficiently. |
| 2624 | if (NumUpdated == 1) { |
| 2625 | unsigned OperandToUpdate = U-OperandList; |
| 2626 | assert(getOperand(OperandToUpdate) == From && |
| 2627 | "ReplaceAllUsesWith broken!"); |
| 2628 | setOperand(OperandToUpdate, ToC); |
| 2629 | } else { |
| 2630 | for (unsigned i = 0, e = getNumOperands(); i != e; ++i) |
| 2631 | if (getOperand(i) == From) |
| 2632 | setOperand(i, ToC); |
| 2633 | } |
Chris Lattner | b64419a | 2005-10-03 22:51:37 +0000 | [diff] [blame] | 2634 | return; |
| 2635 | } |
| 2636 | } |
| 2637 | |
| 2638 | // Otherwise, I do need to replace this with an existing value. |
Chris Lattner | c4062ba | 2005-10-03 21:58:36 +0000 | [diff] [blame] | 2639 | assert(Replacement != this && "I didn't contain From!"); |
| 2640 | |
Chris Lattner | 7a1450d | 2005-10-04 18:13:04 +0000 | [diff] [blame] | 2641 | // Everyone using this now uses the replacement. |
| 2642 | uncheckedReplaceAllUsesWith(Replacement); |
Chris Lattner | c4062ba | 2005-10-03 21:58:36 +0000 | [diff] [blame] | 2643 | |
| 2644 | // Delete the old constant! |
| 2645 | destroyConstant(); |
| 2646 | } |
| 2647 | |
| 2648 | void ConstantStruct::replaceUsesOfWithOnConstant(Value *From, Value *To, |
Chris Lattner | 7a1450d | 2005-10-04 18:13:04 +0000 | [diff] [blame] | 2649 | Use *U) { |
Chris Lattner | c4062ba | 2005-10-03 21:58:36 +0000 | [diff] [blame] | 2650 | assert(isa<Constant>(To) && "Cannot make Constant refer to non-constant!"); |
Chris Lattner | 8760ec7 | 2005-10-04 01:17:50 +0000 | [diff] [blame] | 2651 | Constant *ToC = cast<Constant>(To); |
| 2652 | |
Chris Lattner | dff5911 | 2005-10-04 18:47:09 +0000 | [diff] [blame] | 2653 | unsigned OperandToUpdate = U-OperandList; |
| 2654 | assert(getOperand(OperandToUpdate) == From && "ReplaceAllUsesWith broken!"); |
| 2655 | |
Jim Laskey | c03caef | 2006-07-17 17:38:29 +0000 | [diff] [blame] | 2656 | std::pair<StructConstantsTy::MapKey, Constant*> Lookup; |
Chris Lattner | 8760ec7 | 2005-10-04 01:17:50 +0000 | [diff] [blame] | 2657 | Lookup.first.first = getType(); |
| 2658 | Lookup.second = this; |
| 2659 | std::vector<Constant*> &Values = Lookup.first.second; |
| 2660 | Values.reserve(getNumOperands()); // Build replacement struct. |
Chris Lattner | c4062ba | 2005-10-03 21:58:36 +0000 | [diff] [blame] | 2661 | |
Chris Lattner | dff5911 | 2005-10-04 18:47:09 +0000 | [diff] [blame] | 2662 | |
Chris Lattner | 8760ec7 | 2005-10-04 01:17:50 +0000 | [diff] [blame] | 2663 | // Fill values with the modified operands of the constant struct. Also, |
| 2664 | // compute whether this turns into an all-zeros struct. |
Chris Lattner | dff5911 | 2005-10-04 18:47:09 +0000 | [diff] [blame] | 2665 | bool isAllZeros = false; |
| 2666 | if (!ToC->isNullValue()) { |
| 2667 | for (Use *O = OperandList, *E = OperandList+getNumOperands(); O != E; ++O) |
| 2668 | Values.push_back(cast<Constant>(O->get())); |
| 2669 | } else { |
| 2670 | isAllZeros = true; |
| 2671 | for (Use *O = OperandList, *E = OperandList+getNumOperands(); O != E; ++O) { |
| 2672 | Constant *Val = cast<Constant>(O->get()); |
| 2673 | Values.push_back(Val); |
| 2674 | if (isAllZeros) isAllZeros = Val->isNullValue(); |
| 2675 | } |
Chris Lattner | 8760ec7 | 2005-10-04 01:17:50 +0000 | [diff] [blame] | 2676 | } |
Chris Lattner | dff5911 | 2005-10-04 18:47:09 +0000 | [diff] [blame] | 2677 | Values[OperandToUpdate] = ToC; |
| 2678 | |
Chris Lattner | 8760ec7 | 2005-10-04 01:17:50 +0000 | [diff] [blame] | 2679 | Constant *Replacement = 0; |
| 2680 | if (isAllZeros) { |
| 2681 | Replacement = ConstantAggregateZero::get(getType()); |
| 2682 | } else { |
| 2683 | // Check to see if we have this array type already. |
Owen Anderson | 5c96ef7 | 2009-07-07 18:33:04 +0000 | [diff] [blame] | 2684 | sys::SmartScopedWriter<true> Writer(*ConstantsLock); |
Chris Lattner | 8760ec7 | 2005-10-04 01:17:50 +0000 | [diff] [blame] | 2685 | bool Exists; |
Jim Laskey | c03caef | 2006-07-17 17:38:29 +0000 | [diff] [blame] | 2686 | StructConstantsTy::MapTy::iterator I = |
Chris Lattner | 69edc98 | 2006-09-28 00:35:06 +0000 | [diff] [blame] | 2687 | StructConstants->InsertOrGetItem(Lookup, Exists); |
Chris Lattner | 8760ec7 | 2005-10-04 01:17:50 +0000 | [diff] [blame] | 2688 | |
| 2689 | if (Exists) { |
| 2690 | Replacement = I->second; |
| 2691 | } else { |
| 2692 | // Okay, the new shape doesn't exist in the system yet. Instead of |
| 2693 | // creating a new constant struct, inserting it, replaceallusesof'ing the |
| 2694 | // old with the new, then deleting the old... just update the current one |
| 2695 | // in place! |
Chris Lattner | 69edc98 | 2006-09-28 00:35:06 +0000 | [diff] [blame] | 2696 | StructConstants->MoveConstantToNewSlot(this, I); |
Chris Lattner | 8760ec7 | 2005-10-04 01:17:50 +0000 | [diff] [blame] | 2697 | |
Chris Lattner | dff5911 | 2005-10-04 18:47:09 +0000 | [diff] [blame] | 2698 | // Update to the new value. |
| 2699 | setOperand(OperandToUpdate, ToC); |
Chris Lattner | 8760ec7 | 2005-10-04 01:17:50 +0000 | [diff] [blame] | 2700 | return; |
| 2701 | } |
Chris Lattner | c4062ba | 2005-10-03 21:58:36 +0000 | [diff] [blame] | 2702 | } |
| 2703 | |
Chris Lattner | c4062ba | 2005-10-03 21:58:36 +0000 | [diff] [blame] | 2704 | assert(Replacement != this && "I didn't contain From!"); |
| 2705 | |
Chris Lattner | 7a1450d | 2005-10-04 18:13:04 +0000 | [diff] [blame] | 2706 | // Everyone using this now uses the replacement. |
| 2707 | uncheckedReplaceAllUsesWith(Replacement); |
Chris Lattner | c4062ba | 2005-10-03 21:58:36 +0000 | [diff] [blame] | 2708 | |
| 2709 | // Delete the old constant! |
| 2710 | destroyConstant(); |
| 2711 | } |
| 2712 | |
Reid Spencer | d84d35b | 2007-02-15 02:26:10 +0000 | [diff] [blame] | 2713 | void ConstantVector::replaceUsesOfWithOnConstant(Value *From, Value *To, |
Chris Lattner | 7a1450d | 2005-10-04 18:13:04 +0000 | [diff] [blame] | 2714 | Use *U) { |
Chris Lattner | c4062ba | 2005-10-03 21:58:36 +0000 | [diff] [blame] | 2715 | assert(isa<Constant>(To) && "Cannot make Constant refer to non-constant!"); |
| 2716 | |
| 2717 | std::vector<Constant*> Values; |
| 2718 | Values.reserve(getNumOperands()); // Build replacement array... |
| 2719 | for (unsigned i = 0, e = getNumOperands(); i != e; ++i) { |
| 2720 | Constant *Val = getOperand(i); |
| 2721 | if (Val == From) Val = cast<Constant>(To); |
| 2722 | Values.push_back(Val); |
| 2723 | } |
| 2724 | |
Reid Spencer | d84d35b | 2007-02-15 02:26:10 +0000 | [diff] [blame] | 2725 | Constant *Replacement = ConstantVector::get(getType(), Values); |
Chris Lattner | c4062ba | 2005-10-03 21:58:36 +0000 | [diff] [blame] | 2726 | assert(Replacement != this && "I didn't contain From!"); |
| 2727 | |
Chris Lattner | 7a1450d | 2005-10-04 18:13:04 +0000 | [diff] [blame] | 2728 | // Everyone using this now uses the replacement. |
| 2729 | uncheckedReplaceAllUsesWith(Replacement); |
Chris Lattner | c4062ba | 2005-10-03 21:58:36 +0000 | [diff] [blame] | 2730 | |
| 2731 | // Delete the old constant! |
| 2732 | destroyConstant(); |
| 2733 | } |
| 2734 | |
| 2735 | void ConstantExpr::replaceUsesOfWithOnConstant(Value *From, Value *ToV, |
Chris Lattner | 7a1450d | 2005-10-04 18:13:04 +0000 | [diff] [blame] | 2736 | Use *U) { |
Chris Lattner | c4062ba | 2005-10-03 21:58:36 +0000 | [diff] [blame] | 2737 | assert(isa<Constant>(ToV) && "Cannot make Constant refer to non-constant!"); |
| 2738 | Constant *To = cast<Constant>(ToV); |
| 2739 | |
| 2740 | Constant *Replacement = 0; |
| 2741 | if (getOpcode() == Instruction::GetElementPtr) { |
Chris Lattner | b5d7030 | 2007-02-19 20:01:23 +0000 | [diff] [blame] | 2742 | SmallVector<Constant*, 8> Indices; |
Chris Lattner | c4062ba | 2005-10-03 21:58:36 +0000 | [diff] [blame] | 2743 | Constant *Pointer = getOperand(0); |
| 2744 | Indices.reserve(getNumOperands()-1); |
| 2745 | if (Pointer == From) Pointer = To; |
| 2746 | |
| 2747 | for (unsigned i = 1, e = getNumOperands(); i != e; ++i) { |
| 2748 | Constant *Val = getOperand(i); |
| 2749 | if (Val == From) Val = To; |
| 2750 | Indices.push_back(Val); |
| 2751 | } |
Chris Lattner | b5d7030 | 2007-02-19 20:01:23 +0000 | [diff] [blame] | 2752 | Replacement = ConstantExpr::getGetElementPtr(Pointer, |
| 2753 | &Indices[0], Indices.size()); |
Dan Gohman | 12fce77 | 2008-05-15 19:50:34 +0000 | [diff] [blame] | 2754 | } else if (getOpcode() == Instruction::ExtractValue) { |
Dan Gohman | 12fce77 | 2008-05-15 19:50:34 +0000 | [diff] [blame] | 2755 | Constant *Agg = getOperand(0); |
Dan Gohman | 12fce77 | 2008-05-15 19:50:34 +0000 | [diff] [blame] | 2756 | if (Agg == From) Agg = To; |
| 2757 | |
Dan Gohman | 1ecaf45 | 2008-05-31 00:58:22 +0000 | [diff] [blame] | 2758 | const SmallVector<unsigned, 4> &Indices = getIndices(); |
Dan Gohman | 12fce77 | 2008-05-15 19:50:34 +0000 | [diff] [blame] | 2759 | Replacement = ConstantExpr::getExtractValue(Agg, |
| 2760 | &Indices[0], Indices.size()); |
| 2761 | } else if (getOpcode() == Instruction::InsertValue) { |
Dan Gohman | 12fce77 | 2008-05-15 19:50:34 +0000 | [diff] [blame] | 2762 | Constant *Agg = getOperand(0); |
| 2763 | Constant *Val = getOperand(1); |
Dan Gohman | 12fce77 | 2008-05-15 19:50:34 +0000 | [diff] [blame] | 2764 | if (Agg == From) Agg = To; |
| 2765 | if (Val == From) Val = To; |
| 2766 | |
Dan Gohman | 1ecaf45 | 2008-05-31 00:58:22 +0000 | [diff] [blame] | 2767 | const SmallVector<unsigned, 4> &Indices = getIndices(); |
Dan Gohman | 12fce77 | 2008-05-15 19:50:34 +0000 | [diff] [blame] | 2768 | Replacement = ConstantExpr::getInsertValue(Agg, Val, |
| 2769 | &Indices[0], Indices.size()); |
Reid Spencer | 6c38f0b | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 2770 | } else if (isCast()) { |
Chris Lattner | c4062ba | 2005-10-03 21:58:36 +0000 | [diff] [blame] | 2771 | assert(getOperand(0) == From && "Cast only has one use!"); |
Reid Spencer | 6c38f0b | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 2772 | Replacement = ConstantExpr::getCast(getOpcode(), To, getType()); |
Chris Lattner | c4062ba | 2005-10-03 21:58:36 +0000 | [diff] [blame] | 2773 | } else if (getOpcode() == Instruction::Select) { |
| 2774 | Constant *C1 = getOperand(0); |
| 2775 | Constant *C2 = getOperand(1); |
| 2776 | Constant *C3 = getOperand(2); |
| 2777 | if (C1 == From) C1 = To; |
| 2778 | if (C2 == From) C2 = To; |
| 2779 | if (C3 == From) C3 = To; |
| 2780 | Replacement = ConstantExpr::getSelect(C1, C2, C3); |
Robert Bocchino | 2300448 | 2006-01-10 19:05:34 +0000 | [diff] [blame] | 2781 | } else if (getOpcode() == Instruction::ExtractElement) { |
| 2782 | Constant *C1 = getOperand(0); |
| 2783 | Constant *C2 = getOperand(1); |
| 2784 | if (C1 == From) C1 = To; |
| 2785 | if (C2 == From) C2 = To; |
| 2786 | Replacement = ConstantExpr::getExtractElement(C1, C2); |
Chris Lattner | a93b4b5 | 2006-04-08 05:09:48 +0000 | [diff] [blame] | 2787 | } else if (getOpcode() == Instruction::InsertElement) { |
| 2788 | Constant *C1 = getOperand(0); |
| 2789 | Constant *C2 = getOperand(1); |
| 2790 | Constant *C3 = getOperand(1); |
| 2791 | if (C1 == From) C1 = To; |
| 2792 | if (C2 == From) C2 = To; |
| 2793 | if (C3 == From) C3 = To; |
| 2794 | Replacement = ConstantExpr::getInsertElement(C1, C2, C3); |
| 2795 | } else if (getOpcode() == Instruction::ShuffleVector) { |
| 2796 | Constant *C1 = getOperand(0); |
| 2797 | Constant *C2 = getOperand(1); |
| 2798 | Constant *C3 = getOperand(2); |
| 2799 | if (C1 == From) C1 = To; |
| 2800 | if (C2 == From) C2 = To; |
| 2801 | if (C3 == From) C3 = To; |
| 2802 | Replacement = ConstantExpr::getShuffleVector(C1, C2, C3); |
Reid Spencer | ee3c991 | 2006-12-04 05:19:50 +0000 | [diff] [blame] | 2803 | } else if (isCompare()) { |
| 2804 | Constant *C1 = getOperand(0); |
| 2805 | Constant *C2 = getOperand(1); |
| 2806 | if (C1 == From) C1 = To; |
| 2807 | if (C2 == From) C2 = To; |
| 2808 | if (getOpcode() == Instruction::ICmp) |
| 2809 | Replacement = ConstantExpr::getICmp(getPredicate(), C1, C2); |
Chris Lattner | eab4926 | 2008-07-14 05:17:31 +0000 | [diff] [blame] | 2810 | else { |
Nick Lewycky | a21d3da | 2009-07-08 03:04:38 +0000 | [diff] [blame] | 2811 | assert(getOpcode() == Instruction::FCmp); |
| 2812 | Replacement = ConstantExpr::getFCmp(getPredicate(), C1, C2); |
Chris Lattner | eab4926 | 2008-07-14 05:17:31 +0000 | [diff] [blame] | 2813 | } |
Chris Lattner | c4062ba | 2005-10-03 21:58:36 +0000 | [diff] [blame] | 2814 | } else if (getNumOperands() == 2) { |
| 2815 | Constant *C1 = getOperand(0); |
| 2816 | Constant *C2 = getOperand(1); |
| 2817 | if (C1 == From) C1 = To; |
| 2818 | if (C2 == From) C2 = To; |
| 2819 | Replacement = ConstantExpr::get(getOpcode(), C1, C2); |
| 2820 | } else { |
Torok Edwin | 56d0659 | 2009-07-11 20:10:48 +0000 | [diff] [blame] | 2821 | LLVM_UNREACHABLE("Unknown ConstantExpr type!"); |
Chris Lattner | c4062ba | 2005-10-03 21:58:36 +0000 | [diff] [blame] | 2822 | return; |
| 2823 | } |
| 2824 | |
| 2825 | assert(Replacement != this && "I didn't contain From!"); |
| 2826 | |
Chris Lattner | 7a1450d | 2005-10-04 18:13:04 +0000 | [diff] [blame] | 2827 | // Everyone using this now uses the replacement. |
| 2828 | uncheckedReplaceAllUsesWith(Replacement); |
Chris Lattner | c4062ba | 2005-10-03 21:58:36 +0000 | [diff] [blame] | 2829 | |
| 2830 | // Delete the old constant! |
| 2831 | destroyConstant(); |
Matthijs Kooijman | ba5d7ef | 2008-07-03 07:46:41 +0000 | [diff] [blame] | 2832 | } |
Nick Lewycky | 49f8919 | 2009-04-04 07:22:01 +0000 | [diff] [blame] | 2833 | |
Nick Lewycky | b8f9b7a | 2009-05-10 20:57:05 +0000 | [diff] [blame] | 2834 | void MDNode::replaceElement(Value *From, Value *To) { |
| 2835 | SmallVector<Value*, 4> Values; |
| 2836 | Values.reserve(getNumElements()); // Build replacement array... |
| 2837 | for (unsigned i = 0, e = getNumElements(); i != e; ++i) { |
| 2838 | Value *Val = getElement(i); |
| 2839 | if (Val == From) Val = To; |
Nick Lewycky | 49f8919 | 2009-04-04 07:22:01 +0000 | [diff] [blame] | 2840 | Values.push_back(Val); |
| 2841 | } |
Nick Lewycky | b8f9b7a | 2009-05-10 20:57:05 +0000 | [diff] [blame] | 2842 | |
| 2843 | MDNode *Replacement = MDNode::get(&Values[0], Values.size()); |
Nick Lewycky | 49f8919 | 2009-04-04 07:22:01 +0000 | [diff] [blame] | 2844 | assert(Replacement != this && "I didn't contain From!"); |
Nick Lewycky | b8f9b7a | 2009-05-10 20:57:05 +0000 | [diff] [blame] | 2845 | |
Nick Lewycky | 49f8919 | 2009-04-04 07:22:01 +0000 | [diff] [blame] | 2846 | uncheckedReplaceAllUsesWith(Replacement); |
Nick Lewycky | b8f9b7a | 2009-05-10 20:57:05 +0000 | [diff] [blame] | 2847 | |
Nick Lewycky | 49f8919 | 2009-04-04 07:22:01 +0000 | [diff] [blame] | 2848 | destroyConstant(); |
| 2849 | } |