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